Kev*_*lan 1 flutter flutter-navigation flutter-routes flutter-getx
我有一个 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)
这是我用来将数据发送到下一页的方法
GestureDetector(
onTap: () {
Get.to(SecondPage(), arguments: [
controller._phoneNumberController.text
]);
},
child: Container(
height: 40,
width: double.infinity,
decoration: BoxDecoration(
color: Color(0xffEF5E41),
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding:
const EdgeInsets.only(right: 130),
child: Text(
"Proceed",
style: TextStyle(
color: Color(0xffFCFCFC),
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
),
Padding(
padding:
const EdgeInsets.only(right: 16),
child: Icon(
Icons.arrow_forward_ios,
color: Color(0xffFCFCFC),
),
)
],
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
这是我尝试打印从第一页发送的数据的地方
dynamic argumentData = Get.arguments;
@override
void onInit() {
print(argumentData[0]);
super.onInit();
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题
我知道你在这里有一个公认的答案,但只是想用一种不同的、可以说更简单的方法来补充,这种方法不涉及传递参数或密钥。
任何TextEditingController位于 GetX 类中的内容都可以从任何地方访问。
添加一个监听器并将该值存储到一个变量中。
class Controller extends GetxController {
final textController = TextEditingController();
RxString controllerText = ''.obs;
@override
void onInit() {
super.onInit();
textController.addListener(() {
controllerText.value = textController.text;
});
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用几个简单的页面来测试这一点。
class Page1 extends StatelessWidget {
static const id = '/page1';
final controller = Get.put(Controller());
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextField(controller: controller.textController),
ElevatedButton(
onPressed: () => Get.toNamed(Page2.id),
child: Text('Go to page 2'),
),
],
),
),
);
}
}
class Page2 extends StatelessWidget {
static const id = '/page2';
@override
Widget build(BuildContext context) {
final controller = Get.find<Controller>();
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Center(
// Obx technically isn't needed for this example because
// Page2 builds after the value is updated
child: Obx(
() => Text(controller.controllerText
.value),
),
),
ElevatedButton(
onPressed: () => Get.back(),
child: Text('Go to page 1'),
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13977 次 |
| 最近记录: |