首页 > 解决方案 > 在 Json 数据中更新后未显示 ListView

问题描述

状态响应代码为 200,但 ListView 未显示并卡在 CircularProgressIndicator 上。当我在 Json 数据中有 2 个项目时,它显示得很好,但是当我添加另一个项目时,它没有显示出来!我尝试删除 ListView.separated 并改用 ListView.builder 并尝试使用 StreamBuilder 但我似乎没有正确使用它,因为我对 Flutter 还很陌生。如果我必须使用 StreamBuilder,我可以在这里指导如何正确使用它吗?谢谢你。

这是我的 API_manager.dart 文件:

import 'package:http/http.dart' as http;
import 'package:aritic/models/contactsModel.dart';

// ignore: camel_case_types
class API_Manager {
  Future<ContactsModel> getContacts() async {
    var client = http.Client();
    var contactsModel;
    String contacts_url =
        'https://exampleapi.com';
    String basicAuth = 'Basic auth key example';
    try {
      var response = await client.get(contacts_url,
          headers: <String, String>{'authorization': basicAuth});
      print(response.statusCode);
      if (response.statusCode == 200) {
        var jsonString = response.body;
        var jsonMap = json.decode(jsonString);
        contactsModel = ContactsModel.fromJson(jsonMap);
      }
    } catch (Exception) {
      return contactsModel;
    }
    return contactsModel;
  }
}

我的用户界面代码:

import 'package:aritic/models/contactsModel.dart';
import 'package:aritic/services/api_manager.dart';

class ContactsPage extends StatefulWidget {
  @override
  _ContactsPageState createState() => _ContactsPageState();
}

class _ContactsPageState extends State<ContactsPage>
    with SingleTickerProviderStateMixin {
  Future<ContactsModel> _contactsModel;
  TabController _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: 2, initialIndex: 0, vsync: this);
    _tabController.addListener(_handleTabIndex);
    _contactsModel = API_Manager().getContacts();
  }

  @override
  void dispose() {
    _tabController.removeListener(_handleTabIndex);
    _tabController.dispose();
    super.dispose();
  }

  void _handleTabIndex() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Contacts'),
            bottom: PreferredSize(
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: TabBar(
                    controller: _tabController,
                    isScrollable: true,
                    unselectedLabelColor: Colors.white.withOpacity(0.3),
                    indicatorColor: Colors.white,
                    tabs: [
                      Tab(
                        child: Text('Contacts'),
                      ),
                      Tab(
                        child: Text('Companies'),
                      )
                    ],
                  ),
                ),
                preferredSize: Size.fromHeight(40.0)),
            actions: <Widget>[
              Padding(
                padding: const EdgeInsets.only(right: 16.0),
                child: IconButton(
                  icon: Icon(Icons.search),
                  color: Colors.white,
                  onPressed: () {},
                ),
              ),
            ],
          ),
          body: TabBarView(controller: _tabController, children: <Widget>[
            Container(
                height: double.infinity,
                child: FutureBuilder<ContactsModel>(
                    future: _contactsModel,
                    builder: (BuildContext context,
                        AsyncSnapshot<ContactsModel> snapshot) {
                      if (snapshot.hasData) {
                        return ListView.separated(
                          shrinkWrap: true,
                          padding: const EdgeInsets.all(6),
                          itemCount: snapshot.data.contacts.length,
                          itemBuilder: (BuildContext context, int index) {
                            List keys = snapshot.data.contacts.keys.toList();
                            List values =
                                snapshot.data.contacts.values.toList();
                            var contact = values[index];
                            return InkWell(
                              onTap: () {
                                Navigator.push(context,
                                    MaterialPageRoute(builder: (_) => ViewContact()));
                              },
                              child: Container(
                                height: 50,
                                color: Colors.white,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Text(
                                      contact.owner.firstName +
                                          " " +
                                          contact.owner.lastName,
                                      style: TextStyle(fontSize: 16),
                                    ),
                                    Text(
                                      contact.owner.username,
                                      style: TextStyle(fontSize: 14),
                                    ),
                                    SizedBox(
                                      height: 5,
                                    ),
                                  ],
                                ),
                              ),
                            );
                          },
                          separatorBuilder: (BuildContext context, int index) {
                             return SizedBox(
                              height: 5,
                            );
                          },
                        );
                      } else
                        return Center(
                            child: CircularProgressIndicator(
                                backgroundColor: Colors.blueGrey[700],
                                valueColor: AlwaysStoppedAnimation<Color>(
                                    Colors.cyan)));
                    })),
            Container(
                height: double.infinity,
                child: ListView(
                  padding: const EdgeInsets.all(6),
                  children: <Widget>[
                    InkWell(
                      onTap: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (_) => ViewCompany()));
                      },
                      child: Container(
                        height: 50,
                        color: Colors.white,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              'example company name',
                              style: TextStyle(fontSize: 16),
                            ),
                            Text(
                              'example company domain',
                              style: TextStyle(fontSize: 14),
                            )
                          ],
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 5,
                    ),
                    InkWell(
                      onTap: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (_) => ViewCompany()));
                      },
                      child: Container(
                        height: 50,
                        color: Colors.white,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              'example company name',
                              style: TextStyle(fontSize: 16),
                            ),
                            Text(
                              'example company domain',
                              style: TextStyle(fontSize: 14),
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                )),
          ]),
          floatingActionButton: _bottomButtons(),
        ));
  }

  Widget _bottomButtons() {
    return _tabController.index == 0
        ? FloatingActionButton(
            shape: StadiumBorder(),
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(builder: (_) {
                return AddContacts();
              }));
            },
            backgroundColor: Colors.cyan,
            child: Icon(
              Icons.person_add,
              color: Colors.white,
            ))
        : FloatingActionButton(
            shape: StadiumBorder(),
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(builder: (_) {
                return AddCompany();
              }));
            },
            backgroundColor: Colors.cyan,
            child: Icon(
              Icons.add,
              color: Colors.white,
            ),
          );
  }
}

Json 示例(完整的 json 太大,无法在此处上传):

{
    "total": "187144",
    "contacts": {
        "897": {
            "isPublished": true,
        "id": 897,
            "fields": {
                "core": {
                    "points": {
                        "id": "47",
                        "label": "Points"
                    },
                    "firstname": {
                        "id": "2",
                        "label": "First Name",
                        "value": "Jason"
                    },
                    "lastname": {
                        "id": "3",
                        "label": "Last Name",
                        "value": "Lamuda"
                    },
                    "...": {
                             "..." : "..."
                            }
                },
            "ipAddresses": [
                {
                    "ip": "70.127.91.131",
                    "ipDetails": {
                        "city": "Bradenton",
                        "region": "Florida",
                        "timezone": "America/New_York",
                    }
                },
                 "...": {
                             "..." : "..."
                            }

输出屏幕(卡在 CircularProgressIndicator 上): 这里

标签: jsonapiflutterlistviewdart

解决方案


创建一个类似的函数

Future getContacts()async{
      _contactsModel = API_Manager().getContacts();
 }

然后在你的 initState

getContacts().then((value){
  setState((){});
 })

推荐阅读