首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter:“Future<dynamic>”不是bool类型的子类型

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

Stack Overflow用户
提问于 2019-07-27 21:01:40
回答 4查看 15.8K关注 0票数 8

我正在尝试实现一个简单的登录/注销功能。我的场景是这样的:

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

我的问题是,我有一个routeLogin函数,我创建的选择之间的主页和登陆页。我得到了这个错误:

代码语言:javascript
运行
复制
I/flutter ( 9026): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 9026): The following assertion was thrown building MyApp(dirty):
I/flutter ( 9026): type 'Future<dynamic>' is not a subtype of type 'bool'
I/flutter ( 9026):
I/flutter ( 9026): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 9026): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 9026): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 9026):   https://github.com/flutter/flutter/issues/new?template=BUG.md

这是我的代码:

代码语言:javascript
运行
复制
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();
    }
    
  }
}

请让我知道我哪里出错了,我是新来的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-07-27 21:49:59

您可以使用future builder轻松获得此行为。

代码语言:javascript
运行
复制
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();
        }
      },
    ),
  );
}
票数 13
EN

Stack Overflow用户

发布于 2019-07-27 21:30:53

假设loginCheck中的getBool函数返回未来,

您正在尝试将一个未来放入一个布尔值中。

将该行更改为:

代码语言:javascript
运行
复制
checkValue = await loginCheck.getBool("login");
票数 5
EN

Stack Overflow用户

发布于 2020-11-09 13:16:38

异步函数必须返回Future<>。下面是一个如何做到这一点的示例。

首先创建getLoginStatus()函数

代码语言:javascript
运行
复制
Future<bool> getLoginStatus() async {
  try {
    var isLogin = SharedPref.pref.getBool('isLogin');
    return isLogin != null ? true : false;
  } catch (e) {
    print(e);
    return false;
  }
}

在像这样调用该函数之后

代码语言:javascript
运行
复制
routeLogin() {
  getLoginStatus().then((isLogin) {
    print("isLogin == $isLogin");
    if (isLogin) {
      navigateToNextScreen(HomePage());
    } else {
      navigateToNextScreen(LoginPage());
    }
  });
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57232397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档