flutter - 输入'未来' 不是类型 '((GetXState) => 无效)?
问题描述
我正在开发一个带有 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 控制器的初始化非常好,我的 usercontroller 代码是:
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 的第一个答案,此错误已被删除
我的飞溅活动代码是
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()
}
});
}
}
解决方案
好吧,您的maininit()
函数是一个函数,但在调用您的小部件async
时您并没有等待它。因此,它不是返回控制器本身(UserController)而是返回一个盒装的initState
GetX
Future<dynamic>.
解决方案是使您的maininit()
函数成为普通函数而不是异步函数。那是:
maininit() {
print("here");
Get.put<UserController>(UserController());
print("here");
Get.put<AuthController>(AuthController());
print("here");
}
如果您只使用Get.put
orGet.lazyPut
而不是Get.putAsync
.
另外,在我看来,大多数时候你会为控制器使用具体的类。因此,在放置控制器时完全没有必要使用泛型类型。所以你maininit()
现在应该是这样的:
maininit() {
print("here");
Get.put(UserController());
print("here");
Get.put(AuthController());
print("here");
}
推荐阅读
- php - Ionic Angular Query 字符串解析以登录
- google-sheets - google sheet api:获取集合中每个电子表格的单元格值
- azure - CosmosDB:高效地从大型容器迁移记录
- python - 在一天中的特定时间运行 Python 函数
- r - 如何在 X 轴上绘制 R 多个变量,在 Y 轴上绘制它们的值?
- lua - Lua 中的串联 AOB
- uwp - 为多实例 uwp 应用程序实现桌面扩展
- json - 如何从 Springboot 更新 mongoDb 中的单个 JSON 字段
- php - Laravel POST 请求返回 204 响应
- python - sklearn中的列转换如何工作?