【问题标题】:type 'Future<dynamic>' is not a subtype of type '((GetXState<DisposableInterface>) => void)?''Future<dynamic>' 类型不是 '((GetXState<DisposableInterface>) => void)?' 类型的子类型
【发布时间】:2021-05-21 16:57:07
【问题描述】:

我正在开发一个带有 GetX 功能的颤振项目,但我肯定遇到了一些不相关的问题,而且根据我的说法,上面提到了令人惊讶的错误。

第一个导致错误的代码是

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");
  }
}

在第一次打印后出现错误

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

Auth 控制器的初始化非常好 我的用户控制器代码是:

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();
  }
}

我对 GetX 了解不多,因为我是第一次使用它,所以请多多包涵。

在此之后我收到以下错误,根据 jahangir 的第一个答案,此错误已被删除

error image

我的启动代码是

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()
    }
    });
  }
}

【问题讨论】:

    标签: flutter exception idisposable flutter-getx


    【解决方案1】:

    好吧,您的 maininit() 函数是一个 async 函数,但是当您调用 GetX 小部件的 initState 时,您并没有等待它。因此,它不是返回控制器本身(UserController),而是返回一个装箱的Future&lt;dynamic&gt;.

    解决方案是让您的 maininit() 函数成为普通函数而不是异步函数。那就是:

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

    如果您只使用Get.putGet.lazyPut 而不是Get.putAsync,那么将其设为异步函数是没有意义的。

    另外,在我看来,大多数时候你会为控制器使用具体的类。因此,在放置控制器时完全没有必要使用泛型类型。所以你的maininit() 现在应该是这样的:

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

    【讨论】:

    • 试过了......这给出了错误说明 Null check operator used on a null value
    • 嗯,这是另一个问题。而且您的页面/小部件代码似乎也很不寻常。你能试试return GetX&lt;AuthController&gt;(initState: maininit(),..然后if (_.user == "") {.. 而不是if (Get.find&lt;AuthController&gt;().user == "") {...
    • 另外,你的maininit()是一个类函数吗?如果是这样,我建议将其设为全局或静态函数
    • 虽然上述问题已修复,但仍然存在同样的错误,但仍然存在空值错误
    • 好的!首先,loadSplashScreen();不应该在myAppState();之后调用吗?请检查您是否从 sharedpref 中获得布尔值而不是 null。如果你这样做,我建议你将loadSplashScreen() 调用移到myAppState()then 内,紧跟在setState 之后。或者,也许您可​​以使用 Getxcontroller 更有效地管理您的启动画面。
    猜你喜欢
    • 2020-01-14
    • 2021-02-06
    • 2021-10-18
    • 2020-09-24
    • 2020-09-11
    • 2020-10-13
    • 2021-02-10
    • 1970-01-01
    • 2021-08-29
    相关资源
    最近更新 更多