【发布时间】:2021-08-11 10:44:44
【问题描述】:
我的代码给出了Null check operator used on a null value 错误,尽管我在代码中找不到任何空值。
我不知道实际上是哪个屏幕导致了错误,但肯定是这两个屏幕中的一个
- 启动画面
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tajicEasy/ui/auth/landingPage.dart';
import 'mySharedPreferences.dart';
import 'onBoarding_page.dart';
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
loadSplashScreen();
myAppState();
}
bool isFirstTimeOpen = false;
myAppState() {
MySharedPreferences.instance
.getBooleanValue("firstTimeOpen")
.then((value) => setState(() {
isFirstTimeOpen = value;
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Image.asset(
"assets/images/logo.png",
width: double.infinity,
),
),
);
}
Future<Timer> loadSplashScreen() async {
return Timer(Duration(seconds: 3), onDoneLoadind);
}
onDoneLoadind() async {
Get.offAll(() => isFirstTimeOpen ? LandingPage() : OnBoardingPage());
}
}
- 加载画面
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:tajicEasy/controller/authController.dart';
import 'package:tajicEasy/controller/userController.dart';
import 'package:tajicEasy/ui/auth/login_in.dart';
import 'package:tajicEasy/ui/widgets/bottomNavigationBar.dart';
class LandingPage extends GetWidget<AuthController> {
@override
Widget build(BuildContext context){
return GetX(initState: maininit(),
builder: (_) {
print("here1");
if (Get.find<AuthController>().user!=null) {
print("here1");
return Login();
} else {
print("here1");
return AppMain();
}
});
}
maininit() {
print("here");
Get.put<UserController>(UserController());
print("here");
Get.put<AuthController>(AuthController());
print("here");
}
}
我尝试在 myAppState() 中的 setState 之后放置 loadSplashScreen();
但在那之后我得到了同样的错误
检查isFirstTimeOpen 不为空
【问题讨论】:
-
出现这种情况是因为我们使用了
bang运算符,即!在空值上。但是这里似乎没有这样的代码。检查您的其他小部件 -
此 (
bang) 运算符未在任何小部件中使用 -
不工作它抛出同样的错误
标签: flutter get nullpointerexception nul flutter-getx