How to pass Json data to another screen in flutter

☆樱花仙子☆ 提交于 2021-01-29 06:00:32

问题


List results;      
setState(() {
          var resBody = json.decode(res.body);
          print('Response body: ${resBody}');
          results = resBody;
        });
        Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SecondScreen(jsondata: results,),
            ));

results is a jsondata and pass it to the second screen as jsondata

In secondscreen how to retrieve jsondata ?


回答1:


This may help you. Let me know

class SecondScreen extends StatefulWidget {

  final List jsondata;

  SecondScreen({Key key, this.jsondata}) : super(key: key);

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

class _SecondScreenState extends State<SecondScreen> {

    // use jsondata here in widget here like this,
**widget.jsondata**

}



回答2:


You can store JsonData “state” in a model class and use it in the other screen.



来源:https://stackoverflow.com/questions/55956470/how-to-pass-json-data-to-another-screen-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!