标签: flutter-getx

Getx onInit 未调用

应用程序卡在启动屏幕上有什么原因吗?我使用getx状态管理。如果 authToken 不为空,则应转到主页。但onInit在控制器类中没有调用。

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: '/splashScreen',
      getPages: [
        GetPage(name: '/splashScreen', page: () => SplashScreen(),binding: Bind()),
        GetPage(
            name: '/login', page: () => LoginPage(), binding:Bind()),
        GetPage(
            name: '/mainPage', page: () => MainPage(), binding:Bind())
      ],
      debugShowCheckedModeBanner: false,
      localizationsDelegates: const [
        LocalizationDelegate(),
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''),
        const Locale('zh', ''),
      ],
      title: 'Sample',
      theme: ThemeData(
        accentIconTheme: …
Run Code Online (Sandbox Code Playgroud)

splash-screen dart flutter flutter-getx

4
推荐指数
1
解决办法
2万
查看次数

在 flutter 中使用 Getx 管理应用程序生命周期状态?

管理应用程序生命周期状态的最佳方法是什么?

您会使用带有 WidgetsBindingObserver 的 getxservice 来做到这一点吗?

提前致谢。

flutter flutter-getx

4
推荐指数
1
解决办法
9908
查看次数

Flutter:如何使用 Getx Obx 更新谷歌地图中的标记位置?

我有以下view

import 'dart:async';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '../../Controllers/mapview_controller.dart';

class MapViewerPage extends StatelessWidget {
  MapViewerPage({Key? key}) : super(key: key);

  final MapViewController mapcontroller = MapViewController();
  final Completer<GoogleMapController> _controller = Completer();
  final CameraPosition _initialCameraPosition = const CameraPosition(
    target: LatLng(28.527582, 77.0688971),
    zoom: 16,
  );

  final BitmapDescriptor defaultIcon =
      BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed);

  final List<Marker> allMarkers = [];

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapViewController>(
        init: mapcontroller,
        builder: (controller) {
           return SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: Obx(() => GoogleMap(
            myLocationButtonEnabled: true,
            markers:Set<Marker>.of(allMarkers),
            mapType: …
Run Code Online (Sandbox Code Playgroud)

dart flutter flutter-getx

4
推荐指数
1
解决办法
4337
查看次数

颤振,GetX 初始化状态

有人可以解释如何使用onInitinGetX以及使用dispose/重要吗onClose

我想要GETapi 数据并从应用程序启动时显示它

已经用谷歌搜索没有找到任何有用的:(

dart flutter flutter-getx

4
推荐指数
1
解决办法
1万
查看次数

Obx ListView 在值更改后不更新

我正在接一个RxList电话todoData。在此列表的基础上,aListView.builder构建一个列表,每个列表中都有一个按钮。单击此按钮时,done相应项目的字段将更新为truefalse。但是,尽管值已更新,但用户界面并没有改变。
清单如下:

class StateController extends GetxController {
  RxList<Todo> todoData = <Todo>[
Todo(
    name: 'todo1',
    done: true),
Todo(
    name: 'todo2',
    done: false),
Todo(
    name: 'todo3',
    done: true),
Todo(
    name: 'todo4',
    done: false),
Todo(
    name: 'todo5',
    done: false)
 ].obs;
}
Run Code Online (Sandbox Code Playgroud)

控制器:

  final StateController _controller = Get.find();
Run Code Online (Sandbox Code Playgroud)

更新功能:

void updateItem(Todo e) {
  /* final int index = _controller.todoData.indexOf(e);
  _controller.todoData[index].done = !e.done; */
  _controller.todoData.firstWhere((Todo i) => i == e).done = !e.done;
  _controller.refresh(); …
Run Code Online (Sandbox Code Playgroud)

flutter flutter-getx

4
推荐指数
1
解决办法
1730
查看次数

如何解决未处理的异常:将对象转换为可编码对象失败?

我创建了 2 个类,第一个是:

class Mesure {
  late String equipement; 
  late String number; 
  late String courant = ""; 
  

  Mesure.init(List<String> values) {
    equipement = values[0];
    number = values[1];
    courant = values[2];
    
  }

}
Run Code Online (Sandbox Code Playgroud)

和数据库类:

class DataBase {
  late int? id;
  late Mesure? mesure;
  late String? status;
  DataBase.init(int id, Mesure mesure, String status) {
    id = id;
    mesure = mesure;
    status = status;
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我用一些值初始化了数据库:

  Mesure mesure = Mesure.init(values);
  DataBase test = DataBase.init(0, mesure, "status");
Run Code Online (Sandbox Code Playgroud)

我想将它存储在存储 getX 中:

   final box = GetStorage(); …
Run Code Online (Sandbox Code Playgroud)

local-storage flutter flutter-getx

4
推荐指数
1
解决办法
1507
查看次数

为什么大部分代码都使用Provider包而不使用GetX/Riverpod/Bloc?

我正在学习颤振。大多数教程使用Providerpackage,但 Youtube 上的其他一些教程使用GetX. 我搜索了一下,发现似乎还有其他软件包,例如BlocRiverpod等等。

我很困惑,不知道该用哪一个?它似乎GetX比导航更简单Provider,并且具有更多功能/特性,例如导航,但是使用它的代码/教程并不多!我想知道为什么?另外我应该使用哪一个?

编辑:也许这看起来像是一个基于意见的问题,但在我看来并非如此。我想知道为什么大多数教程/开源代码仍然使用Providerpackage over GetXRiverpodBloc等?

ProviderFlutter支持的官方包并且使用起来更安全吗?它有更少的错误吗?或者只是因为它是大多数开发人员学会使用的第一个包并且它对他们有用,所以他们不喜欢用更好的包来替换它?

state-management flutter flutter-provider riverpod flutter-getx

4
推荐指数
1
解决办法
9971
查看次数

Flutter GetX - 如何管理控制器删除?

我有一个带有GetX控制器的 Flutter 应用程序。该应用程序有 6 个屏幕,每个屏幕都有其GetxController.

屏幕 1 和 2 用于登录系统,而屏幕 3 至 6 用于应用程序内容。

登录后,用户可以在屏幕 3-4-5 之间前进和后退,但是当他到达屏幕 6 时,他只能转到屏幕 3,并且必须删除所有先前的堆栈(因此他无法返回)。

1st problem:如果我从 Screen 6 执行 a Get.offAll(() => const Screen3()),Screen3 的控制器将被删除,并且不再起作用。我的解决方法(不知道这个词是否存在!:D)通过标记Controller3为永久

Get.put(Controller3(), permanent: true)

但这里来了

2nd problem:如果用户按下logout按钮(仅出现在屏幕 3 中),这次我需要将其Controller3删除。这次,调用Get.offAll不会删除控制器,也不会调用Get.delete<Controller3>(),因为它说

“Controller3”已被标记为永久,SmartManagement 无权删除它。

我陷入这种境地,我真的不知道该怎么办

controller dart flutter flutter-getx

4
推荐指数
1
解决办法
2273
查看次数

您需要调用“Get.put(dynamic())”或“Get.lazyPut(()=&gt;dynamic())”

我收到错误未处理的异常:未找到“动态”。当我调用 authController 时,您需要调用“Get.put(dynamic())”或“Get.lazyPut(()=>dynamic())”,下面是我的代码。

if (Get.find<AuthController>().isLoggedIn()) {
    //statements
  }
Run Code Online (Sandbox Code Playgroud)

我的初始化函数

Future init() async {
// Core
final sharedPreferences = await SharedPreferences.getInstance();
  Get.lazyPut(() => sharedPreferences);

Get.lazyPut(() => ApiClient(
  appBaseUrl: AppConstants.BASE_URL,
));

// Repository
 Get.lazyPut(() => AuthRepo(apiClient:Get.find(), sharedPreferences: Get.find()));

 // controller
Get.lazyPut(() => AuthController(authRepo: Get.find()));
Run Code Online (Sandbox Code Playgroud)

}

主要方法

 void main() async{
  await di.init();
  runApp(
   child: MyApp(),
  ),
  );
 }
Run Code Online (Sandbox Code Playgroud)

flutter flutter-getx

4
推荐指数
1
解决办法
1760
查看次数

Flutter:使用 GetX 刷新 ListView.Builder

我正在List of Cards根据toDoId的数量创建。

toDoController.toDo() 就像

toDo = [q1, r4, g4, d4].obs;
Run Code Online (Sandbox Code Playgroud)

而且,这是我的 ListView.builder()

  Obx(() {
         List _todo = toDoController.toDo();

         return ListView.builder(
         shrinkWrap: true,
         scrollDirection: Axis.horizontal,
         itemCount: _todo.length,
         itemBuilder: (BuildContext context, int i) {
                           
         var _loading = true;
         var _title = 'loading';
                          
         getTodoInfo() async {
         _title = await toDoController
                .getTodoInfo(
                     _todo[i]
                 );
         _loading = false;
         print(_title); // 'Clean!' <--- returns correct title
         }

         getTodoInfo();

         return Container(
           height: 150,
           width: 150,
           child: _loading
           ? Text(
             _title,
             )
             : Text(
             _title, …
Run Code Online (Sandbox Code Playgroud)

flutter getx flutter-getx

3
推荐指数
1
解决办法
4948
查看次数