Home > Back-end >  Flutter - 'type 'Future<dynamic>' is not a subtype of type 'Widget?'&
Flutter - 'type 'Future<dynamic>' is not a subtype of type 'Widget?'&

Time:08-20

I am getting an error. I wrote a code like this:

class MainDart extends StatefulWidget {
  const MainDart({Key? key}) : super(key: key);
  @override
  State<MainDart> createState() => _MainDartState();
}

class _MainDartState extends State<MainDart> {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      themeMode: ThemeMode.dark,
      theme: ThemeData(
        appBarTheme: const AppBarTheme(
          color: Color(0xFF27272A),
          elevation: 0,
          toolbarHeight: 65,
        ),
        scaffoldBackgroundColor: Color(0xFF27272A),
      ),
      home: StreamUser(),
    );
  }

}

StreamUser() {
  FirebaseAuth.instance.authStateChanges().listen((User? user) {
    if (user == null) {
      return Get.offAll(const LoginPage());
    } else {
      return Get.offAll(const HomePage());
    }
  });
}

But I am getting this error:

error message

How can I solve the problem?

CodePudding user response:

If you remove the return statements in your StreamUser() function that should solve it. problem is that that function is supposed to return a type of Void (the function declaration has no return type in front of it) but you are returning the Get.offAll functions which are of type Future.

StreamUser() {
  FirebaseAuth.instance.authStateChanges().listen((User? user) {
    if (user == null) {
        Get.offAll(const LoginPage());
    } else {
        Get.offAll(const HomePage());
    }
  });
}

If you really want to return the values of the Get.offAll functions then you should use

Future<dynamic> StreamUser() {
  FirebaseAuth.instance.authStateChanges().listen((User? user) {
    if (user == null) {
      return Get.offAll(const LoginPage());
    } else {
      return Get.offAll(const HomePage());
    }
  });
}
  • Related