when I print for debug it shows type–>Instance of 'Future<dynamic>' in Flutter

Issue

simply my task is to give a shared data to the variable "type" , please help me out

*this sample code is main.dart and i used shared preferences for data transfer and the key for type is "typeOfUser" , I saw so many blogs and even stackoverflow, Questions but none of them suited for me *

code follows :

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:nativestore/database/datatrans.dart';
import 'package:nativestore/homepage.dart';
import 'package:nativestore/loginout/loginpage.dart';
import 'package:nativestore/shophomepage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'loginout/auth.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: routePage(),
    );
  }
}

class routePage extends StatefulWidget {
  @override
  routePageState createState() => routePageState();
}

class routePageState extends State<routePage> {
  final AuthService _auth = AuthService();
  int type;
  bool isLoggedin = false;
  getIntValuesSF() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int val = prefs.getInt('typeOfUser');
    return val;
  }

  @override
  void initState() {
    super.initState();
    print("type-->${getIntValuesSF()}");
    print("Init state");
    _auth.autoLogin().then((value) {
      if (value == 'null') {
        print(isLoggedin);
        setState(() {
          isLoggedin = false;
        });
      } else if (value != null) {
        setState(() {
          isLoggedin = true;
        });
      } else {
        setState(() {
          isLoggedin = false;
        });
      }
    });
  }

  // var Utype = GetUserType(type:"ko")
  @override
  Widget build(BuildContext context) {
    return isLoggedin == true
        ? (type == 2)
            ? homePage()
            : ShopHomePage()
        : loginPage();
  }
}

output :

Performing hot restart...
Syncing files to device Redmi Note 8...
Restarted application in 2,645ms.
I/flutter (14262): type-->Instance of 'Future<dynamic>'
I/flutter (14262): Init state

so please help me.
Thanks in Advance…

Solution

The reason why your code prints Instance of 'Future<dynamic>' is because the instance of getIntValuesSF() is being printed and not the value it returns. What you can do here is to run the async function and using then to fetch its return value when the future completes.

void initState() {
    super.initState();
    getIntValuesSF().then((value) {
        debugPrint('type-->$value');
        // do functions requiring value parameter
    });
}

Answered By – Omatt

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *