问题描述
我正在尝试实现简单的登录/注销功能.我的情况是这样的:
I was trying to implement a simple login/logout functionality. My scenario is this:
我有2个页面(登录页面和主页),在main.dart中,我正在使用SharedPreferences来检查用户是否已经登录,如果用户已登录,则将布尔值设置为true单击一个按钮.
I have 2 pages ( login page and home page), In the main.dart, I am using SharedPreferences to check if a user has already logged in or not if the user is logged in, I set a boolean value as true on click of a button.
我遇到的问题是,我创建了一个routeLogin函数,以在主页和着陆页之间进行选择.我得到这个错误:
The issue I am having is, I have a routeLogin function that I created to choose between Homepage and Landingpage.And I get this error:
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
I/flutter ( 9026): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════I/flutter ( 9026): The following assertion was thrown building MyApp(dirty):I/flutter ( 9026): type 'Future' 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 substantiallyI/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
这是我的代码:
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的新手.
Please let me know where did I went wrong, I am new to Flutter.
推荐答案
您可以使用以后的构建器轻松获得此行为.
you can use future builder to obtain this behavior easily.
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();
}
}
);
}
这篇关于Flutter:“未来<动态>"不是布尔型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!