颤振垂直视口无界高度错误

Asy*_*yan 5 dart flutter

我做了一个应用程序来显示一个州的医院列表.

这是Main.dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:emas_app/model/accounts_model.dart';

Future<String> _loadAsset() async{
  return await rootBundle.loadString('Assets/accounts.json');
}

Future<Accounts> loadAccounts() async{

  final response = await _loadAsset();
  final jsonResponse = json.decode(response);

  Accounts accounts = new Accounts.fromJson(jsonResponse);

  return accounts;
}

class ProviderList extends StatefulWidget {

  @override
  ListState createState() {
    return new ListState();
  }
}

class ListState extends State<ProviderList> {

  @override
  Widget build(BuildContext context) {

    Widget newbody = new ExpansionTile(
        title: new Text("State Name"),
        children: <Widget>[
          new FutureBuilder<Accounts>(
              future: loadAccounts(),
              builder: (context, snapshot){
                if(snapshot.hasData){

                return new ListView.builder(
                      itemCount: snapshot.data.accountinfo.length,
                      itemBuilder: (context, index){

                        String username = snapshot.data.accountinfo[index].name;
                        String address = snapshot.data.accountinfo[index].street;
                        String lat = snapshot.data.accountinfo[index].coordinates.lat;
                        String lng = snapshot.data.accountinfo[index].coordinates.lng;

                        return new ListTile(
                            title: new Text(username),
                            trailing: new Row(
                              mainAxisSize: MainAxisSize.min,
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                new IconButton(
                                    icon: Icon(Icons.info),
                                    onPressed: null
                                ),
                                new IconButton(
                                    icon: Icon(Icons.directions),
                                    onPressed: null
                                )
                              ],
                            )
                        );
                      });
                }else{
                  return new Center(
                    child: new CircularProgressIndicator(),
                  );
                }
              })
    ]);

    return new Scaffold(
      appBar: new AppBar(title: new Text("Providers")),
      body: newbody
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是显示扩展的ExpansionTile为空的输出:

提供商名单

这是错误抓住:

I/flutter ( 6305): Vertical viewport was given unbounded height.
I/flutter ( 6305): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 6305): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 6305): typically happens when a scrollable widget is nested inside another scrollable widget.
Run Code Online (Sandbox Code Playgroud)

我已经尝试了我可以在Stack Overflow中找到的所有可能的解决方案,通过使用Expanded或Flexible包装ListView.builder但它不起作用.关于如何解决这个问题的任何想法?

Din*_*ian 25

有两种解决方案:

  1. 使用ListView的shrinkWrap属性

    new ListView.builder(
            shrinkWrap: true,
            itemCount: ...
    
    Run Code Online (Sandbox Code Playgroud)

    原因:

    在你的情况下,ListView是在里面ExpansionTile.ExpansionTile将显示扩展并显示有多少孩子在那里.当ListView 放置在ExpansionTile(无界约束小部件)中时,ListView将扩展到 scrollDirection中的最大大小.根据文档,

    If the scroll view does not shrink wrap, then the scroll view will expand
    to the maximum allowed size in the [scrollDirection]. If the scroll view
    has unbounded constraints in the [scrollDirection], then [shrinkWrap] must
    be true.
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用SizedBox为ListView提供固定高度.

    SizedBox(height: 200.0, child: new ListView.builder(...))
    
    Run Code Online (Sandbox Code Playgroud)