Flutter:“ Future <dynamic>”不是bool类型的子类型

时间:2019-07-27 13:01:39

标签: flutter

我正在尝试实现简单的登录/注销功能。我的情况是这样的:

我有2个页面(登录页面和主页),在main.dart中,我正在使用SharedPreferences检查用户是否已经登录,如果用户已登录,则将布尔值设置为true单击一个按钮。

我遇到的问题是,我创建了一个routeLogin函数,以在主页和着陆页之间进行选择。 我得到这个错误:

I / flutter(9026):W小部件库引起的异常CA ═══════════════════════════════ I / flutter(9026):构建MyApp(dirty)时引发了以下断言: I / flutter(9026):类型'Future'不是类型'bool'的子类型 I /颤振(9026): I / flutter(9026):要么断言表明框架本身有错误,要么我们应该提供 I / flutter(9026):此错误消息中的更多信息可帮助您确定并解决根本原因。 I / flutter(9026):无论哪种情况,请通过在GitHub上提交错误来报告此断言: I / flutter(9026):https://github.com/flutter/flutter/issues/new?template=BUG.md

这是我的代码:

import 'package:credit/src/pages/landing.dart';
import 'package:flutter/material.dart';
import 'package:credit/src/pages/credit/home.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

 bool checkValue;
 checkLoginValue () async{
   SharedPreferences loginCheck = await SharedPreferences.getInstance();
   checkValue = loginCheck.getBool("login");
   }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test App',
       debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: routeLogin());
      //home: LandingPage());      
  }

  routeLogin()
  {
    print("Check value");
    if (checkValue == null){
      return LandingPage();
    }
    else{
      return HomePage();
    }

  }
}

请让我知道我哪里出了问题,我是Flutter的新手。

4 个答案:

答案 0 :(得分:1)

您可以使用将来的构建器轻松获得此行为。

Future<bool> checkLoginValue () async{
   SharedPreferences loginCheck = await SharedPreferences.getInstance();
   return loginCheck.getBool("login");
   }

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'Test App',
       debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:FutureBuilder<bool>(
            future: checkLoginValue,
             builder:(BuildContext context, AsyncSnapshot<bool> snapshot){
           if (snapshot.data == false){
                    return LandingPage();
    }
    else{
      return HomePage();
    }
}
);

  }

答案 1 :(得分:0)

checkValue的值为Future而不是bool

Future checkValue;

因此,您可以检查它是否返回了值或错误。

routeLogin()
  {
    print("Check value");
    checkValue.then((res) {
      return LandingPage();
    }).catchError((e) {
      return HomePage();
    });
  }

答案 2 :(得分:0)

假设您来自loginCheck的getBool函数返回Future,

您正在尝试将Future放入bool。

将该行更改为:

checkValue = await loginCheck.getBool("login");

答案 3 :(得分:0)

异步函数必须返回Future <>。这是如何执行此操作的示例。

首先创建您的getLoginStatus()函数

Future<bool> getLoginStatus() async {
  try {
    var isLogin = SharedPref.pref.getBool('isLogin');
    return isLogin != null ? true : false;
  } catch (e) {
    print(e);
    return false;
  }
}

像这样调用该函数之后

routeLogin() {
  getLoginStatus().then((isLogin) {
    print("isLogin == $isLogin");
    if (isLogin) {
      navigateToNextScreen(HomePage());
    } else {
      navigateToNextScreen(LoginPage());
    }
  });
}