我正在尝试使用 Flutter 和GetX构建一个用于状态管理的应用程序,在我的一个页面中,我需要在页面中间构建一个 TabView 小部件,我已经找到了很多解释如何构建 TabView 的内容小部件,如这篇文章和这篇文章,但所有这些都扩展了一个带有SingleTickerProviderStateMixin的状态控制器。
据我了解阅读文档,我不应该像那样使用 StatefulWidgets 和状态,但我无法弄清楚如何使用 GetX 架构来制定解决方案。
我已经在我的页面中尝试过:
class CourseDetailPage extends StatelessWidget {
final TabController _tabController = Get.put(TabController(vsync: null, length: 2));
}
Run Code Online (Sandbox Code Playgroud)
和
class CourseDetailPage extends StatelessWidget {
final TabController _tabController = TabController(vsync: null, length: 2);
}
Run Code Online (Sandbox Code Playgroud)
但是 TabController 的 VSYNC 参数不能为空,我不知道我如何无法获得 TickerProvider 来填充它。
我可以使用 GetX 查看导航器堆栈吗?我查看了文档,但找不到有关此主题的任何内容。我通常会关闭这样的对话框
Get.until((route) => !Get.isDialogOpen);
Run Code Online (Sandbox Code Playgroud)
但我想知道如果特定页面的实例位于路由历史记录中,我是否可以关闭路由,如下所示
Get.until((route) => !Get.routingHistory.contains('/someRoute'));
Run Code Online (Sandbox Code Playgroud)
请注意,这不是有效的语法。
我有这个全局绑定类来初始化一些服务,我需要立即初始化它:
import 'package:get/get.dart';
import 'package:vepo/data/data_provider/local_data_provider.dart';
import 'package:vepo/data/data_source/local_data_source.dart';
import 'services/authentication_service.dart';
class GlobalBindings extends Bindings {
final LocalDataProvider _localDataProvider = LocalDataProvider();
@override
void dependencies() {
Get.put<AuthenticationService>(AuthenticationService(), permanent: true);
Get.put<LocalDataProvider>(_localDataProvider, permanent: true);
Get.put<LocalDataSource>(LocalDataSource(_localDataProvider),
permanent: true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是在我的初始绑定中:
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Vepo',
initialRoute: AppPages.INITIAL,
initialBinding: GlobalBindings(),
transitionDuration: const Duration(milliseconds: 500),
defaultTransition: Transition.rightToLeft,
getPages: AppPages.routes,
home: Root(),
theme: homeTheme,
);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在类构造函数中,我尝试“找到”它:
class UserLocalRepository extends VpService implements IUserLocalRepository {
UserLocalRepository() {
localDataSource = …Run Code Online (Sandbox Code Playgroud) 我正在使用以下包https://pub.dev/packages/get。我需要在 GetxController 的 onClose 中关闭我的 .obs 吗?我在文档中找不到任何关于此的信息。看看我的记忆,它似乎正在被自动销毁。
我想遍历的Future<list>,但得到的错误,如果我尝试使用循环或如果我试图将其设置List。
有没有其他方法可以循环遍历列表中的每个元素:
Future<List> getDailyTask(DateTime date) async {
var params = {'date': date.toIso8601String()};
Uri uri = Uri.parse('URL');
final newURI = uri.replace(queryParameters: params);
http.Response response = await http.get(
newURI,
headers: {"Accept": "application/json"},
);
List foo = json.decode(response.body);
return foo;
}
Run Code Online (Sandbox Code Playgroud) 正如标题所说,有几种方法可以更新状态。我什么时候应该选择一个?
这是我的完整代码...
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class DialogHelper{
//show error dialog
static void showErrorDialog({String title='error',String description='Something went wrong'})
{
Get.dialog(
Dialog(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(title,style: Get.textTheme.headline4,),
Text(description,style: Get.textTheme.headline6,),
ElevatedButton(onPressed: () {
if (Get.isDialogOpen) Get.back();
},
child: Text('okay')),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
19:25:错误:“bool?”类型的值 无法分配给“bool”类型的变量,因为“bool?” 可以为空,而 'bool' 则不能。if (Get.isDialogOpen) Get.back();
如果条件 Get.isDialogOpen 线上出现错误