【问题标题】:how to load Json to flutter listview如何将 Json 加载到颤动的列表视图中
【发布时间】:2019-11-16 00:41:49
【问题描述】:

我是 Flutter 新手,我正在开发一个 Flutter 应用程序,我必须在列表中显示数据库中的数据。

我得到 JSON 格式的数据。

{
    "data": [
        {
            "id": “1”,
            "testo": "Fare la spesa",
            "stato": "1"
        },
        {
            "id": “2”,
            "testo": "Portare fuori il cane",
            "stato": "1"
        },
        {
            "id": “3”,
            "testo": “jhon”,
            "stato": "0"
        }
    ]
}

问题是我没有加载数据,我不明白怎么做。

我应该用数组读取“数据”,但我不知道该怎么做。

谁能帮我弄清楚怎么做?

感谢您的帮助。

PS。我试过this教程

Main.dart

Future<Post> fetchPost() async {
  final response =
  await http.get('http://simone.fabriziolerose.it/index.php/Hello/dispdataflutter');

  if (response.statusCode == 200) {
    // If the call to the server was successful, parse the JSON.
    return Post.fromJson(json.decode(response.body));
  } else {
    // If that call was not successful, throw an error.
    throw Exception('Failed to load post');
  }
}

class Post {
  final int id;
  final String title;
  final String body;

  Post({ this.id, this.title, this.body});

  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(

      id: json['id'],
      title: json['testo'],
      body: json['stato'],
    );
  }
}

void main() => runApp(MyApp(post: fetchPost()));

class MyApp extends StatelessWidget {
  final Future<Post> post;

  MyApp({Key key, this.post}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fetch Data Example'),
        ),
        body: Center(
          child: FutureBuilder<Post>(
            future: post,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data.title);
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }

              // By default, show a loading spinner.
              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: json listview flutter


    【解决方案1】:

    我宁愿从 fetchPost() 返回帖子列表,如下所示:

    Future<List<Post>> fetchPost() async {
      final response = await http.get('http://simone.fabriziolerose.it/index.php/Hello/dispdataflutter');
    
      if (response.statusCode == 200) {
      List responseList = json.decode(response.body);
      return responseList.map((data)=>Post.fromJson(data)).toList();
      } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
      }
    }
    

    并像这样使用 ListView.builder

    List<Post> post;
    
    child: ListView.builder(
      itemCount: post.length,
      physics: BouncingScrollPhysics(),
      padding: EdgeInsets.all(0),
      itemBuilder: (context, index){
        return ListAdapter(
           id: post[index].id,
           title: post[index].title,
           body: post[index].body
           );
        },
      )
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2022-01-18
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2020-11-20
      • 2020-07-21
      • 2020-12-24
      相关资源
      最近更新 更多