You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Get.to传参后详情页数据不更新问题求助

问题原因及解决方案

核心原因

  1. 全局变量未重新初始化
    你把data定义为类外部的全局变量:
final DebrisPeopleModel data = Get.arguments;

这个变量只会在第一次加载页面时从Get.arguments获取值,后续再次进入页面时,全局变量不会重新读取新的路由参数,始终保留第一次的旧数据。

  1. TextFormField的initialValue特性限制
    TextFormField的initialValue仅在组件第一次初始化时生效,即使后续传入的value发生变化,因为组件已构建完成且设置了enabled: false,不会自动触发内容更新。

解决方案

方案一:将参数移至State类,在生命周期中更新

把data变量移到State类内部,通过didChangeDependencies生命周期方法每次进入页面时重新获取参数并触发UI更新:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safearea/Model/DebrisPeople.dart';

class UnderRublePeopleDetailPage extends StatefulWidget {
  const UnderRublePeopleDetailPage({super.key});
  @override
  State<UnderRublePeopleDetailPage> createState() =
      _UnderRublePeopleDetailPageState();
}

class _UnderRublePeopleDetailPageState
    extends State<UnderRublePeopleDetailPage> {
  late DebrisPeopleModel data;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 每次进入页面重新获取路由参数
    data = Get.arguments;
    setState(() {}); // 触发UI重建,更新TextFormField内容
  }

  @override
  Widget build(BuildContext context) {
    print(data.nameSurname);
    return Scaffold(
      appBar: AppBar(
        title: const Text("Depremzede Bilgisi"),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left: 12, right: 12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Center(
              child: Icon(
                Icons.person,
                size: 150,
                color: Color(0xFFCB3126),
              ),
            ),
            Row(
              children: [
                FieldText("Ad-Soyad:"),
                InformationTextField(
                  context,
                  data.nameSurname.toString(),
                ),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("Telefon Numarası:"),
                InformationTextField(
                  context,
                  data.phone.toString(),
                ),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("İl / İlçe / Mahalle:"),
                InformationTextField(
                  context,
                  "${data.city} / ${data.district} / ${data.neighbourhood}",
                ),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("Adres:"),
                InformationTextField(
                  context,
                  data.adress.toString(),
                ),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [FieldText("")],
            ),
          ],
        ),
      ),
    );
  }

  Widget FieldText(String value) {
    return Expanded(
      flex: 2,
      child: Text(
        value,
        style: const TextStyle(fontSize: 20),
      ),
    );
  }

  Widget InformationTextField(BuildContext context, String value) {
    return Expanded(
      flex: 6,
      child: SizedBox(
        child: TextFormField(
          enabled: false,
          initialValue: value,
          style: TextStyle(fontSize: 18, fontFamily: "Roboto"),
          decoration: InputDecoration(
            contentPadding: EdgeInsets.only(left: 20, right: 0),
            fillColor: const Color.fromARGB(255, 237, 237, 237),
            filled: true,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(50),
              borderSide: const BorderSide(
                color: Color(0xFFCB3126),
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(50),
              borderSide: const BorderSide(
                color: Color(0xFFCB3126),
                width: 1.5,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

方案二:使用GetX响应式变量(更符合GetX生态)

通过GetX的响应式变量监听数据变化,自动更新UI:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safearea/Model/DebrisPeople.dart';

class UnderRublePeopleDetailPage extends StatefulWidget {
  const UnderRublePeopleDetailPage({super.key});
  @override
  State<UnderRublePeopleDetailPage> createState() =
      _UnderRublePeopleDetailPageState();
}

class _UnderRublePeopleDetailPageState
    extends State<UnderRublePeopleDetailPage> {
  // 定义响应式变量
  final RxString nameSurname = "".obs;
  final RxString phone = "".obs;
  final RxString location = "".obs;
  final RxString address = "".obs;

  @override
  void initState() {
    super.initState();
    updateDisplayData();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    updateDisplayData();
  }

  // 更新响应式变量值
  void updateDisplayData() {
    final newData = Get.arguments as DebrisPeopleModel;
    nameSurname.value = newData.nameSurname.toString();
    phone.value = newData.phone.toString();
    location.value = "${newData.city} / ${newData.district} / ${newData.neighbourhood}";
    address.value = newData.adress.toString();
  }

  @override
  Widget build(BuildContext context) {
    print(nameSurname.value);
    return Scaffold(
      appBar: AppBar(
        title: const Text("Depremzede Bilgisi"),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left: 12, right: 12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Center(
              child: Icon(
                Icons.person,
                size: 150,
                color: Color(0xFFCB3126),
              ),
            ),
            Row(
              children: [
                FieldText("Ad-Soyad:"),
                // Obx监听变量变化,自动更新UI
                Obx(() => InformationTextField(context, nameSurname.value)),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("Telefon Numarası:"),
                Obx(() => InformationTextField(context, phone.value)),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("İl / İlçe / Mahalle:"),
                Obx(() => InformationTextField(context, location.value)),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [
                FieldText("Adres:"),
                Obx(() => InformationTextField(context, address.value)),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: [FieldText("")],
            ),
          ],
        ),
      ),
    );
  }

  Widget FieldText(String value) {
    return Expanded(
      flex: 2,
      child: Text(
        value,
        style: const TextStyle(fontSize: 20),
      ),
    );
  }

  Widget InformationTextField(BuildContext context, String value) {
    return Expanded(
      flex: 6,
      child: SizedBox(
        child: TextFormField(
          enabled: false,
          initialValue: value,
          style: TextStyle(fontSize: 18, fontFamily: "Roboto"),
          decoration: InputDecoration(
            contentPadding: EdgeInsets.only(left: 20, right: 0),
            fillColor: const Color.fromARGB(255, 237, 237, 237),
            filled: true,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(50),
              borderSide: const BorderSide(
                color: Color(0xFFCB3126),
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(50),
              borderSide: const BorderSide(
                color: Color(0xFFCB3126),
                width: 1.5,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者Emir Bolat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 18:45:20