Flutter 如何在加载main()时设置检查连接的bool值,然后使用它显示哪个屏幕
我尝试的是在方法之外使用变量,并通过调用方法来设置它,但它不起作用。当我像检查连接一样检查时?SplashScreen():WarningScreen()然后显示错误,如“Future”没有实例方法“call”Flutter 如何在加载main()时设置检查连接的bool值,然后使用它显示哪个屏幕,flutter,dart,Flutter,Dart,我尝试的是在方法之外使用变量,并通过调用方法来设置它,但它不起作用。当我像检查连接一样检查时?SplashScreen():WarningScreen()然后显示错误,如“Future”没有实例方法“call” import'包:连接性/连接性.dart'; 进口“包装:颤振/材料.省道”; 导入“package:algorithm_send_location/pages/home_screen.dart”; 导入“package:algorithm_send_location/pages/sp
import'包:连接性/连接性.dart';
进口“包装:颤振/材料.省道”;
导入“package:algorithm_send_location/pages/home_screen.dart”;
导入“package:algorithm_send_location/pages/splash_screen.dart”;
导入“package:algorithm_send_location/pages/initial_warning.dart”;
导入“包:地理定位器/地理定位器.dart”;
变量路由={
“/home”:(构建上下文)=>主屏幕(),
};
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
bool-accessible=false;
类MyAppState扩展了状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文构建上下文){
_检查连通性;
返回材料PP(
主题:主题数据(
primaryColor:Colors.琥珀色,accentColor:Colors.green[200]),
debugShowCheckedModeBanner:false,
主页:_检查连接?SplashScreen():警告屏幕(),
路线:路线;;
}
}
获取\u checkConnectivity异步{
var internetResult=等待连接()。检查连接();
bool locationResult=等待地理定位器()。isLocationServiceEnabled();
如果(internetResult==ConnectivityResult.none | |!locationResult){
//可访问=错误;
//打印(无障碍);
返回false;
}否则{
//可访问=真;
//打印(无障碍);
返回true;
}
}
如果要使用未来的
结果来构建UI,请使用FutureBuilder
例如
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
未来连接;
@凌驾
void initState(){
_checkConnectivity=_onCheckConnectivity();
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“材料应用程序”,
主页:_buildHome(),
);
}
小部件_buildHome(){
回归未来建设者(
未来:_检查连通性,
生成器:(上下文,快照){
交换机(快照.连接状态){
案例连接状态。完成:
最终连接=snapshot.data;
返回连接?SplashScreen():警告屏幕();
默认值://检查连接时返回加载指示器
返回脚手架(
正文:中(
孩子:大小盒子(
宽度:56.0,
身高:56.0,
子对象:CircularProgressIndicator(),
),
),
);
}
},
);
}
未来_onCheckConnectivity(){
//这里的异步操作
}
}
请注意,我们需要在FutureBuilder
中使用Future
变量作为Future
参数。根据:
未来建设者
未来必须更早获得,例如在State.initState、State.didUpdateConfig或State.didChangeDependencies期间。在构造FutureBuilder时,不能在State.build或stateWidget.build方法调用期间创建它。如果future与FutureBuilder同时创建,则每次重建FutureBuilder的父级时,异步任务都将重新启动
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:algorithm_send_location/pages/home_screen.dart';
import 'package:algorithm_send_location/pages/splash_screen.dart';
import 'package:algorithm_send_location/pages/initial_warning.dart';
import 'package:geolocator/geolocator.dart';
var routes = <String, WidgetBuilder>{
"/home": (BuildContext context) => HomeScreen(),
};
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
bool accessible = false;
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext buildContext) {
_checkConnectivity;
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.amber, accentColor: Colors.green[200]),
debugShowCheckedModeBanner: false,
home: _checkConnectivity ? SplashScreen() : WarningScreen(),
routes: routes);
}
}
get _checkConnectivity async {
var internetResult = await Connectivity().checkConnectivity();
bool locationResult = await Geolocator().isLocationServiceEnabled();
if (internetResult == ConnectivityResult.none || !locationResult) {
//accessible = false;
//print(accessible);
return false;
} else {
//accessible = true;
//print(accessible);
return true;
}
}