本文介绍了在 flutter getx 中对空值使用的空检查运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码给出了 Null check operator used on a null value
错误,尽管我在代码中找不到任何空值.
My code is giving the Null check operator used on a null value
error although I can't found any null value in the code.
我不知道哪个屏幕实际上导致了错误,但肯定是在这两个屏幕之外
I don't know which screen is actually causing the error but surely it is out of these two screens only which are
- 启动画面
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");
}
}
我尝试将 loadSplashScreen();
放在 myAppState()
中的 setState 之后但在那之后我得到了同样的错误
I have tried placing loadSplashScreen();
after setState in myAppState()
but after that I getting the same error
检查isFirstTimeOpen
是否为空
推荐答案
尝试使用GetWidget在启动屏幕上或使用 globalkey 或两者
try to use GetWidget<'controller'> on splash screen or use globalkey or both
这篇关于在 flutter getx 中对空值使用的空检查运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!