Null check operator used on a null value in flutter getx
Asked Answered
P

3

5

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

  1. Splash Screen
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. LoadingScreen
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");
  }
}

I have tried placing loadSplashScreen(); after setState in myAppState() but after that I getting the same error

checked that isFirstTimeOpen is not null

Petrie answered 22/5, 2021 at 13:8 Comment(4)
This arises because we use the bang operator, which is ! on a null value. But there seems to be no such code here. Check your other widgetsLegislator
this (bang) operator is not used in any widgetPetrie
Check if this helps #64279095Legislator
not worked it's throwing same errorPetrie
V
8

When using getx package you need to add GetMaterialApp() in your main.dart

replace this code in your main.dart

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

with this

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
      ...
      return GetMaterialApp(
      ...
      );
  }
Valentine answered 3/9, 2021 at 15:33 Comment(0)
D
4

add the controller to the widget Getx

so this

return GetX(initState:  maininit(),

will be like this

return GetX<AuthController>(initState:  maininit(),
Dock answered 5/10, 2021 at 8:30 Comment(1)
That actually worked for me. Would be nice to understand what Is going on. Can someone explain? Or recommend resources to read/watch?Materfamilias
F
2

try to use GetWidget<'controller'> on splash screen or use globalkey or both

Florence answered 30/5, 2021 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.