【问题标题】:Null check operator used on a null value in flutter getx在flutter getx中对空值使用空检查运算符
【发布时间】:2021-08-11 10:44:44
【问题描述】:

我的代码给出了Null check operator used on a null value 错误,尽管我在代码中找不到任何空值。

我不知道实际上是哪个屏幕导致了错误,但肯定是这两个屏幕中的一个

  1. 启动画面
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());
  }
}
  1. 加载画面
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) 运算符未在任何小部件中使用
  • 检查这是否有帮助 stackoverflow.com/questions/64278595/…
  • 不工作它抛出同样的错误

标签: flutter get nullpointerexception nul flutter-getx


【解决方案1】:

使用 getx 包时,您需要在 main.dart 中添加 GetMaterialApp()

在你的 main.dart 中替换这段代码

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
     ...
     return MaterialApp(
     ...
     );
  }

有了这个

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
      ...
      return GetMaterialApp(
      ...
      );
  }

【讨论】:

    【解决方案2】:

    尝试在初始屏幕上使用 GetWidget 或使用 globalkey 或两者都使用

    【讨论】:

      【解决方案3】:

      将控制器添加到小部件 Getx

      所以这个

      return GetX(initState:  maininit(),
      

      会是这样的

      return GetX<AuthController>(initState:  maininit(),
      

      【讨论】:

        猜你喜欢
        • 2021-08-13
        • 2021-11-11
        • 1970-01-01
        • 2021-11-10
        • 2022-07-04
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 2021-10-29
        相关资源
        最近更新 更多