我想阻止用户使用 getX 返回登录屏幕
onPressed: () {
Get.to(() => HomeScreen());
},
Run Code Online (Sandbox Code Playgroud) 我有一个 flutter 应用程序,可以使用屏幕中的文本表单字段捕获数据。我有第二个屏幕,我想在其中显示从第一个屏幕的文本表单字段捕获的数据。
但首先我想从第二个屏幕控制器将其打印到终端,并查看已传递的内容。我尝试使用 GetX naviagtion 并传递,但出现此错误
The method '[]' was called on null.
Receiver: null
Tried calling: [](0)
Run Code Online (Sandbox Code Playgroud)
这是我用来获取数据作为输入的文本表单字段
TextFormField(
controller: controller._phoneNumberController,
keyboardType: TextInputType.text,
validator: (value) => validatePhoneNumber(value!),
style: TextStyle(
color: accentColor,
fontSize: 15,
fontFamily: 'PoppinsRegular',
),
decoration: InputDecoration(
hintText: "Phone Number",
hintStyle: TextStyle(
fontSize: 14,
color: Colors.black45,
fontWeight: FontWeight.w500),
fillColor: Color(0xffEBEDF4),
filled: true,
border: InputBorder.none,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: Color(0xff1D275C),
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: BorderSide(
color: formFieldColor,
width: 2.0,
),
),
), …Run Code Online (Sandbox Code Playgroud) 我有 GetXController 和 RxList,当我使用列表作为参数创建小部件时(这是唯一可观察的),我收到错误
[Get] the improper use of a GetX has been detected.
Run Code Online (Sandbox Code Playgroud)
控制器看起来像这样:
class Controller extends GetxController {
static Controller get to => Get.find();
final RxList<int> numbers = <int>[].obs;
}
Run Code Online (Sandbox Code Playgroud)
用法是这样的:
class ShowWidget extends StatelessWidget {
ShowWidget({Key? key}) : super(key: key);
final Controller c = Controller.to;
@override
Widget build(BuildContext context) {
return Container(
height: 300,
width: 300,
color: Colors.red,
child: (Obx(
() => ItemsList(numbers: c.numbers),
)),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我添加列表长度作为参数,它似乎工作正常,但我试图了解为什么这不起作用。
完整示例在这里https://pastebin.com/WrQqqsPx
谢谢你的帮助。
bool download=false; setState(() {download=true;});download==true?Text("new"):("old");
Run Code Online (Sandbox Code Playgroud)
如何在没有状态小部件的情况下使用 get X 编写此代码。
谢谢;
我有 flutter 应用程序,当我在其他控制器中使用一个控制器时,我的应用程序中有许多控制器
所以有人建议我使用绑定,但是当我使用 binging 并使用 get.put 方法时,它说我控制器未初始化,任何人都可以建议我如何在 flutter 中使用出价
我在 Flutter 中有一个用户模型类,我用它来解析用户数据。它看起来像这样:
class UserModel {
final String id;
final String username;
final String email;
UserModel({
required this.id,
required this.username,
required this.email,
});
factory UserModel.fromDocument(DocumentSnapshot doc) {
return UserModel(
id: doc['id'],
email: doc['email'],
username: doc['username'],
);
}
}
Run Code Online (Sandbox Code Playgroud)
当用户登录应用程序时,我从 firebase 获取数据并使用类的工厂方法解析它,然后我在应用程序中的几乎所有屏幕中使用它。现在我试图使用 flutter 中的“Get”库使其可观察,但由于某种原因我无法使用 .obs。有什么建议么?
假设我有一个这样的控制器:
class ProfileController extends GetxController {
Rx<UserFacebookInfo?> facebookInfo = null.obs;
void facebookSync() async {
//
// logic to get user info from facebook
//
facebookInfo.value = UserFacebookInfo.fromFacebookApi(userData);
// facebookInfo = UserFacebookInfo.fromFacebookApi(userData).obs; <=== also tried this
update();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在小部件中我有这样的东西:
Widget buildFacebook() => Padding(
padding: const EdgeInsets.only(top: 30.0, right: 20.0, left: 20.0, bottom: 10.0),
child: Obx(() => (_profileController.facebookInfo.value == null) ? Column(
children: [
IconButton(
icon : const Icon(Icons.facebook_outlined, size: 40.0),
onPressed: () => _profileController.facebookSync()
),
const Text(
'Facebook',
style: …Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一个extension函数GetInterfaceas
extension FindOrPut on GetInterface {
S findOrPut<S>(S dependency,{String? tag}){
if(GetInstance().isRegistered<S>(tag: tag)){
return GetInstance().find<S>(tag: tag);
}
return GetInstance().put<S>(dependency,tag: tag);
}
}
Run Code Online (Sandbox Code Playgroud)
与GETxextension Inst on GetInterface团队开发的相同。但我的代码没有按预期工作。
注意:如果我将扩展功能代码放在将要使用它的同一个文件中;效果很好。但如果我把上面的代码放在单独的文件函数中,在 Get 上找不到。
我正在学习在 Flutter 中使用 Getx 库,我遇到了以下问题:想法是有一个 TextFields 列表,其中每个 TextFields 都是可水平扩展的,也就是说,它在屏幕上占用的大小由键入的单词的大小,随着单词大小的增加,TextField 的大小也会增加。如果我添加一行,通过FloatingActionButton,没有问题,我可以顺利打字,但是当我添加第二行时,出现以下错误:
\n\n\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 捕获异常通过小部件库 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\ x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\ xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\ x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\ x90\xe2\x95\x90\xe2\x95\x90\n构建 NewLineKeyword(dirty) 时抛出以下断言:\n在构建期间调用 setState() 或 markNeedsBuild()。
\n
\n\n此 GetX 小部件无法标记为需要构建,因为框架已经在构建小部件的过程中。仅当小部件的祖先之一当前正在构建时,才可以将其标记为需要在构建阶段构建。允许此异常,因为框架在子窗口之前构建父窗口小部件,这意味着将始终构建脏后代。否则,框架可能不会在此构建阶段访问此小部件。\n调用 setState() 或 markNeedsBuild() 的小部件是:GetX<NewLineKeywordController>
\n
\n\n控制器: null\ntag: null\nhas builder\nstate: GetXState#ae1d4(controller: Instance of \'NewLineKeywordController\')\n当进行有问题的调用时当前正在构建的小部件是: NewLineKeyword\ndirty\n相关错误-导致小部件是\nNewLineKeyword
\n
下图显示了运行时的情况:
\n\n下面是可水平扩展的 TextField 的代码:
\nclass NewLineKeyword extends GetView<NewLineKeywordController>{\n final Key myKey;\n\n const NewLineKeyword({ \n Key? key, \n required this.myKey, \n }) : super(key: key);\n\n @override\n Widget build(BuildContext context) …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 getx,我有一个 mainBianding 页面:
\nclass MainBinding implements Bindings {\n @override\n Future<void> dependencies() async {\n Get.putAsync<HiveService>(() => HiveService().init(), permanent: true);\n Get.lazyPut<HomeController>(\n () => HomeController(\n\n dbclient: Get.find<HiveService>()),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n我有一个用于初始化 Hive 的 GETXService
\nclass HiveService extends GetxService {\n late Box<Model> vBox;\n\n Future<HiveService> init() async {\n final appDocumentDirectory =\n await path_provider.getApplicationDocumentsDirectory();\n Hive\n ..init(appDocumentDirectory.path)\n ..registerAdapter<Model>(ModelAdaptor())\n\n return this;\n }\nRun Code Online (Sandbox Code Playgroud)\n午餐后,App HomePage 和 HomeController 将启动,但我收到此错误:
\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following message was thrown building Builder:\n"HiveService" not found. You …Run Code Online (Sandbox Code Playgroud)