GridView在Listview.Builder中引发错误

时间:2020-06-16 11:03:23

标签: flutter

我正在尝试测试一个简单的Fultter应用程序,但它仅显示一个图标而不是3,并且在调试时出现以下错误。

在performLayout()期间引发了以下断言: 'package:flutter / src / rendering / sliver_grid.dart':失败的断言:第160行pos 15: 'childMainAxisExtent!= null && childMainAxisExtent> = 0':不正确。

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

_launchURL(String url) async {
  print(url);
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

class MyApp extends StatelessWidget {
  var details = {
    'data/icon1.png': 'https://www.google.com/',
    'data/icon.png': 'https://www.linkedin.com/',
    'data/icon.png': 'https://www.linkedin2.com/'
  };
  //sites.put();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("BOOKMARKS"),
        ),
        body: Container(
          height: 400,
          color: Colors.white30,
          child: new ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: details.length,
            itemBuilder: (BuildContext context, int index) {
              String key = details.keys.elementAt(index);
              print(key);
              return new GridView.count(
                shrinkWrap: true,
                crossAxisCount: 4,
                childAspectRatio: 1.0,
                padding: const EdgeInsets.all(4.0),
                mainAxisSpacing: 4.0,
                crossAxisSpacing: 4.0,
                children: <Widget>[
                  GestureDetector(
                    onTap: () => _launchURL(details[key]),
                    child: Image.asset(key),
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}

0 个答案:

没有答案