0

I am working on a flutter project with GetX features in it but certainly I got some irrelevant and also according to me surprising error which is mentioned above.

the first error causing code is

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 == "") {
        print("here1");
        return Login();
      } else {
        print("here1");
        return AppMain();
      }
    });
  }

  maininit() async {
    print("here");
    await Get.put<UserController>(UserController());
    print("here");
    await Get.put<AuthController>(AuthController());
    print("here");
  }
}

I am getting error after first here is printed that is in

await Get.put<UserController>(UserController());

Auth controller is initializing absolutely fine my code for usercontroller is:

import 'package:get/get.dart';
import 'package:tajicEasy/model/userModel.dart';
import 'package:tajicEasy/services/database.dart';

class UserController extends GetxController{
  var _userModel = UserModel().obs;

  UserModel get user => _userModel.value;

  set user(UserModel value) => this._userModel.value;
  Database _db = Database();

  bool enable = false;

  enabled(bool value) {
    print("here u");
    enable = !value;
    update();
  }

  void clear() {
    print("here u");
    _userModel.value = UserModel();
  }
}

I don't know much about GetX as I am working on it for the first time so please be lenient on me.

After this I am getting following error this error was removed as per the first answer from jahangir

error image

my code of splash activity is

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tajicEasy/ui/auth/landingPage.dart';
import 'package:tajicEasy/ui/pages/home_page.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();
    myAppState();
  }

  bool isFirstTimeOpen = false;

  myAppState() {
    MySharedPreferences.instance
        .getBooleanValue("firstTimeOpen")
        .then((value) => setState(() {
              isFirstTimeOpen = value;
            })).then((value) => loadSplashScreen());
  }

  @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(){
    Get.offAll(() => {
      if(isFirstTimeOpen == true){
        LandingPage()
      }else{
        HomePage()
    }
    });
  }
}

yogender
  • 202
  • 3
  • 12

1 Answers1

0

Well, your maininit() function is an async function but you are not awaiting for it when calling in the initState of your GetX widget. So instead of returning the controller itself (UserController) it returns a boxed Future<dynamic>.

The solution is to make your maininit() function a normal function instead of an async function. That is:

  maininit() {
   print("here");
   Get.put<UserController>(UserController());
   print("here");
   Get.put<AuthController>(AuthController());
   print("here");
  }

There's no point making it an async function if you just use Get.put or Get.lazyPut instead of Get.putAsync.

Also, in my opinion most of the time you will use concrete classes for controllers. Therefore, it's completely unnecessary to use generic types when putting your controllers. So your maininit() should now look like this:

maininit() {
   print("here");
   Get.put(UserController());
   print("here");
   Get.put(AuthController());
   print("here");
  }
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
  • Tried it.... this give the error stating Null check operator used on a null value – yogender May 22 '21 at 11:31
  • Well, that's another problem. And your Page/Widget code seems unusual too. Can you please try `return GetX(initState: maininit(),..` and then `if (_.user == "") {.. ` instead of `if (Get.find().user == "") {... ` – S. M. JAHANGIR May 22 '21 at 11:42
  • Also, is your `maininit()` is a class function? If so, I would recommend to make it a global or static function – S. M. JAHANGIR May 22 '21 at 11:45
  • still same error though the above problem is fixed but null value error is still there – yogender May 22 '21 at 11:47
  • Okay! First of all, shouldn't `loadSplashScreen();` be called after `myAppState();`? And please check that you are getting a boolean value and not null from your sharedpref. If you do, I would recommend you to move your `loadSplashScreen()` call inside of `then` of `myAppState()` right after the `setState`. Or maybe you could manage your splash screen more efficiently by using Getxcontroller. – S. M. JAHANGIR May 22 '21 at 12:15
  • I have also tried calling ```loadSplashScreen();``` after setState then I got new error saying unexpected format you can see it in updataed code above for splash screen – yogender May 22 '21 at 12:40
  • I think you should move your logic to a controller. It will be more clean, simple and easy to understand. Also I am not sure if the `initState` method of a stateful widget can be made async or not but `onInit()` of a Getxcontroller surely can. Anyway, I think we are going off from the original topic. If you need further help regarding splash screen problem, you should create a new question. Oh! And don't forget to share that link here. – S. M. JAHANGIR May 22 '21 at 12:46
  • https://stackoverflow.com/questions/67649858/null-check-operator-used-on-a-null-value-in-flutter-getx – yogender May 22 '21 at 13:09
  • this is the link yo new question brother – yogender May 22 '21 at 13:09