简体   繁体   English

仅将 _id 传递给下一个 Screen Flutter

[英]Pass only _id to next Screen Flutter

I have a List of items when i click on the an item it will go to next screen so i can show corresponding details of clicked item,when i tried to pass to it pass the entire object data is passed to next page当我点击一个项目时,我有一个项目列表,它会转到下一个屏幕,因此我可以显示单击项目的相应详细信息,当我尝试传递给它时,整个对象数据将传递到下一页

Json Data i showing but i need to pass only _id that contain i the Area_Id object to next screen我显示的 Json 数据,但我只需要将包含 Area_Id 对象的 _id 传递到下一个屏幕

"Area_Id": {
            "_id": "5e27ffbccde0456455857c27",
            "Area_Name": "Morning meeting photo"
          },



"AreaList": [
    {
      "Report_Time_Type": 1,
      "WeakDay": "Friday",
      "_id": "5e27ffc0cde0456455857c4f",
      "Branch_Id": {
        "_id": "5e27ffb7cde0456455857c21",
        "Name": "CHERTHALA",
        "Order_Id": 4
      },
      "Section_Id": {
        "_id": "5e27ffb9cde0456455857c25",
        "Section_Head": "Sales",
        "Order_Id": 1
      },
      "Area_Id": {
        "_id": "5e27ffbccde0456455857c27",
        "Area_Name": "Morning meeting photo"
      },
      "From_Time": "9:00 AM",
      "To_Time": "9:05 AM"
    },
]

This is first page Code Flutter View这是第一页代码颤振视图

         return Column(
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ImageScreen(data.Area_Id)));
                },
                child: Card(
                  elevation: 15,
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          Expanded(
                            flex: 3,
                            child: Padding(
                              padding: EdgeInsets.all(30),
                              child: Text(
                                data.Area_Name.toString(),
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 14,
                                    color: Colors.black87),
                              ),
                            ),
                          ),

                        ],
                      )
                    ],
                  ),
                ),
              ),

  Future<String> getDailyList() async {
    Future dailystatus = SharedPrefrence().getbranchId();
    Future daily = SharedPrefrence().getsectionId();
    Future token = SharedPrefrence().getToken();

    dailystatus.then((data) async {
      var branchid = data;

      daily.then((data) async {
        var section_id = data;

        token.then((data) async {
          var token = data;
          var response = await http.post(Urls.BRANCH_MAPPER,
              headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer $token",
              },
              body: json.encode({
                "Report_Time_Type": "1",
                "Branch_Id": branchid,
                "Section_Id": section_id,
              }));


          print('Respone ${response.body}');
          if (response.statusCode == 200) {
            try {
              var resp = response.body;
              Map<String, dynamic> value = json.decode(resp);
              var report = value['AreaList'];
              for (int i = 0; i < report.length; i++) {
                var data = report[i];
                var areaName = data["Area_Id"]["Area_Name"];
                Dailylist.add(DailyModel.fromJson(data, areaName));
              }
              setState(() {
                array_lenth = Dailylist.length;
              });
            } catch (e) {
              e.toString();
            }
          }
        });
      });
    });
  }
}

and This Model of first page和首页的这个模型

   class  DailyModel {

      String id;
      String From_Time;
      String To_Time;
      String Area_Name;
      String WeakDay;
      String Report_Time_Type;
      String Area_Id;

      DailyModel({this.id,this.From_Time,this.To_Time,this.Area_Name,this.WeakDay,this.Report_Time_Type,this.Area_Id});

      DailyModel.fromJson(json, areaName)
          : id = json['_id'].toString(),
            From_Time = json['From_Time'].toString(),
            To_Time = json['To_Time'].toString(),
            WeakDay = json['WeakDay'].toString(),
            Area_Id = json['_Id'].toString(),
            Report_Time_Type = json['Report_Time_Type'].toString(),
            Area_Name = areaName;
    }

I do believe this is correct ImageModel data = Imagelist[index];我相信这是正确的ImageModel data = Imagelist[index];

Maybe you should remove ImageScreen class.也许您应该删除ImageScreen类。

You can do this instead你可以这样做

class BranchDailyScreen extends StatefulWidget {
  @override
  _BranchDailyScreenState createState() => _BranchDailyScreenState();
}

class _BranchDailyScreenState extends State<BranchDailyScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Branch Daily Screen'),
      ),
      body: Container(
        padding: const EdgeInsets.all(8),
        child: ListView.builder(
            itemCount: 10,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ImageScreen(area_id: '$index'))),
                child: Card(
                  child: Text('Item# $index'),
                ),
              );
            }),
      ),
    );
  }
}

and then to pass to the image screen然后传递到图像屏幕

class ImageScreen extends StatefulWidget {
  final String area_id;

  const ImageScreen({Key key, this.area_id}) : super(key: key);

  @override
  _ImageScreenState createState() => _ImageScreenState();
}

class _ImageScreenState extends State<ImageScreen> {
  String image = 'https://picsum.photos/250?image=';

  @override
  Widget build(BuildContext context) {
  print(widget.area_id);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.area_id),
      ),
      body: Center(
        child: Image.network('$image${widget.area_id}'),
      ),
    );
  }
}

In your Model definition在您的模型定义中

class  DailyModel {

      String id;
      String From_Time;
      String To_Time;
      String Area_Name;
      String WeakDay;
      String Report_Time_Type;
      String Area_Id;

      DailyModel({this.id,this.From_Time,this.To_Time,this.Area_Name,this.WeakDay,this.Report_Time_Type,this.Area_Id});

      DailyModel.fromJson(json, areaName)
          : id = json['_id'].toString(),
            From_Time = json['From_Time'].toString(),
            To_Time = json['To_Time'].toString(),
            WeakDay = json['WeakDay'].toString(),
            Area_Id = json['_Id'].toString(),
            Report_Time_Type = json['Report_Time_Type'].toString(),
            Area_Name = areaName;
    }

I think you mistyped我想你打错了

        Area_Id = json['_Id'].toString(),

It should be它应该是

        Area_Id = json['_id'].toString(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们在颤动中导航到下一个屏幕时传递上下文? - why we pass context when we are navigating to next screen in flutter? Flutter:尝试根据 DropDownButton 输入将变量传递到下一个屏幕 - Flutter : Trying pass a variable to next screen based on DropDownButton input 如何将表单中的数据传递到 flutter 中的下一个屏幕? - How to pass the data from the form to the next screen in flutter? 如何将从 json 获取的多个数据传递到颤振中的下一个屏幕? - How to pass multiple data fetched from json to next screen in flutter? 如何将数据从列表视图传递到下一个屏幕,如抖动图像 - How pass Data to next screen from listview like image in flutter 如何在 flutter 中使用 navigatorKey 传递值下一个屏幕? - How to pass value next screen using navigatorKey in flutter? 如何将变量 id 传递到 flutter 中的第二个屏幕? - How can I pass variable id to second screen in flutter? 仅当上一个屏幕完成加载后,如何导航到 Flutter 中的下一个屏幕? - How to navigate to next screen in Flutter only when the previous screen has finished loading? flutter如何在下一个屏幕运行一个屏幕的功能? - How to run functions of a screen in the next screen With flutter? 如何将一个屏幕作为参数传递给颤动的另一个屏幕? - How to pass a screen as a parameter to another screen in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM