Flutter web ListView.builder啥也没显示

Posted

技术标签:

【中文标题】Flutter web ListView.builder啥也没显示【英文标题】:Flutter web ListView.builder showing nothingFlutter web ListView.builder什么也没显示 【发布时间】:2021-06-13 11:15:57 【问题描述】:

您能帮我解决这个问题,即在下一张图所示的 Flutter Web 应用程序发布后 listview.builder 中没有显示任何内容,尽管在 android 版本中运行良好。 我试图缩短城市列表,但问题仍然存在 Code link Code link

【问题讨论】:

您能否将您的ListView.builder() 包含在代码和错误图像中? 我尝试添加代码,但即使在缩短后仍出现此消息。 '看起来你的帖子主要是代码;请添加更多详细信息。' 我创建了这个 Dartpad dartpad.dev/62deeb2566ace6a783381c081a383654 来测试你的代码。它工作正常注意:您在 listview.builder 中返回 null,所以我将其更改为 SizedBox()。 感谢您的快速回复,问题仅在发布而不是在调试模式下。我已经在 Google play 中发布了这个应用程序,这个问题仍然存在。您可以查看play.google.com/store/apps/details?id=com.shazlycode.weather @AhmedElshazly 所以这个问题只有在构建网络版本时才会发生? 【参考方案1】:

很可能您要么错误地使用了ListView.builder() 方法,要么您正在插入一个具有Expanded() 的子列表,这意味着由于ListView 可以滚动,因此有一个展开的小部件或子组件占用了由于 Flutter 不知道应该占用多少空间,所以基本上不会显示多少空间。

如果您可以分享您的代码,那么找出问题所在会容易得多。

编辑

我刚刚检查了你的 dartpad,我建议你试试下面的代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SearchScreen(),
    );
  


class SearchScreen extends StatefulWidget 
  @override
  _SearchScreenState createState() => _SearchScreenState();


class _SearchScreenState extends State<SearchScreen> 
  TextEditingController _controller = TextEditingController();
  dynamic cityWeatherData;
  bool isLoading = false;
  String cityName = '';
  List cities = [
    City(city: "Cairo", lat: 30.05, long: 31.25),
    City(city: "Alexandria", lat: 31.2, long: 29.95),
    City(city: "Giza", lat: 30.01, long: 31.19),
    City(city: "Ismailia", lat: 30.5903, long: 32.26),
    City(city: "Port Said", lat: 31.26, long: 32.29),
    City(city: "Luxor", lat: 25.7, long: 32.65),
    City(city: "Suhaj", lat: 26.5504, long: 31.7),
    City(city: "Al Mansurah", lat: 31.0504, long: 31.38),
    City(city: "Suez", lat: 30.005, long: 32.5499),
    City(city: "Damanhur", lat: 31.0504, long: 30.47),
    City(city: "Al Minya", lat: 28.09, long: 30.75),
    City(city: "Bani Suwayf", lat: 29.0804, long: 31.09),
    City(city: "Asyut", lat: 27.19, long: 31.1799),
    City(city: "Tanta", lat: 30.7904, long: 31),
    City(city: "Al Fayyum", lat: 29.31, long: 30.84),
    City(city: "Aswan", lat: 24.0875, long: 32.8989),
    City(city: "Kawm Umbu", lat: 24.47, long: 32.95),
    City(city: "Qina", lat: 26.1505, long: 32.72),
    City(city: "Damietta", lat: 31.4204, long: 31.82),
    City(city: "Az Zaqaziq", lat: 30.5833, long: 31.5167),
    City(city: "Mallawi", lat: 27.7304, long: 30.84),
    City(city: "Rosetta", lat: 31.4604, long: 30.39),
    City(city: "Shibin al Kawm", lat: 30.592, long: 30.9),
    City(city: "Al `Arish", lat: 31.1249, long: 33.8006),
    City(city: "Banha", lat: 30.4667, long: 31.1833),
    City(city: "Al Ghardaqah", lat: 27.23, long: 33.83),
    City(city: "Samalut", lat: 28.3004, long: 30.71),
    City(city: "Kafr ash Shaykh", lat: 31.109, long: 30.936),
    City(city: "Jirja", lat: 26.3304, long: 31.88),
    City(city: "Marsa Matruh", lat: 31.3504, long: 27.23),
    City(city: "Al Kharijah", lat: 25.44, long: 30.55),
    City(city: "At Tur", lat: 28.2394, long: 33.6148),
    City(city: "Isna", lat: 25.2904, long: 32.5499),
    City(city: "Bani Mazar", lat: 28.4904, long: 30.81),
    City(city: "Safaja", lat: 26.7337, long: 33.9333),
    City(city: "Siwah", lat: 29.2, long: 25.5167),
    City(city: "Ad Dab`ah", lat: 31.0338, long: 28.4333),
    City(city: "Al `Alamayn", lat: 30.8171, long: 28.95),
    City(city: "As Sallum", lat: 31.567, long: 25.15),
    City(city: "Qasr al Farafirah", lat: 27.0671, long: 27.9666),
    City(city: "Al Qasr", lat: 25.7004, long: 28.8833),
    City(city: "Barnis", lat: 23.946, long: 35.4842),
    City(city: "Jeddah", lat: 21.5169, long: 39.2192),
    City(city: "Ad Dammam", lat: 26.4282, long: 50.0997),
    City(city: "Mecca", lat: 21.43, long: 39.82),
    City(city: "Medina", lat: 24.5, long: 39.58),
    City(city: "At Ta'if", lat: 21.2622, long: 40.3823),
    City(city: "Al Hufuf", lat: 25.3487, long: 49.5856),
    City(city: "Al Hillah", lat: 23.4895, long: 46.7564),
    City(city: "Tabuk", lat: 28.3838, long: 36.555),
    City(city: "Buraydah", lat: 26.3664, long: 43.9628),
    City(city: "Ha'il", lat: 27.5236, long: 41.7001),
    City(city: "Najran", lat: 17.5065, long: 44.1316),
    City(city: "Al Qatif", lat: 26.5196, long: 50.0115),
    City(city: "Al Mubarraz", lat: 25.4291, long: 49.5659),
    City(city: "Al Kharj", lat: 24.1556, long: 47.312),
    City(city: "Yanbu`", lat: 24.0943, long: 38.0493),
    City(city: "Hafar al Batin", lat: 28.4337, long: 45.9601),
    City(city: "Al Jubayl", lat: 27.0046, long: 49.646),
    City(city: "`Ar`ar", lat: 30.99, long: 41.0207),
    City(city: "Abha", lat: 18.2301, long: 42.5001),
    City(city: "Sakaka", lat: 30, long: 40.1333),
    City(city: "Jazan", lat: 16.9066, long: 42.5566),
    City(city: "Al Bahah", lat: 20.0129, long: 41.4677),
    City(city: "Dhahran", lat: 26.2914, long: 50.1583),
    City(city: "Qal`at Bishah", lat: 20.0087, long: 42.5987),
    City(city: "Rafha", lat: 29.6202, long: 43.4948),
    City(city: "Al Wajh", lat: 26.2324, long: 36.4636),
    City(city: "As Sulayyil", lat: 20.4623, long: 45.5722),
    City(city: "Dawmat al Jandal", lat: 29.8153, long: 39.8664),
    City(city: "Al Qurayyat", lat: 31.3333, long: 37.3333),
    City(city: "Al Quway`iyah", lat: 24.0737, long: 45.2806),
    City(city: "Al Qunfudhah", lat: 19.1264, long: 41.0789),
  ];
  List result = [];
  double cityLong = 0;
  double cityLat = 0;

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/city1.jpg'),
              fit: BoxFit.cover,
              colorFilter: ColorFilter.mode(
                  Colors.black.withOpacity(.2), BlendMode.dstATop),
            ),
          ),
          padding: EdgeInsets.all(15),
          child: Column(
            children: <Widget>[
              TextField(
                controller: _controller,
                decoration: InputDecoration(
                  labelText: "Enter City...",
                  prefixIcon: Icon(Icons.search),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(10),
                    ),
                  ),
                ),
                onChanged: (value) 
                  setState(() 
                    cityName = value;
                  );
                ,
              ),
              TextButton(
                onPressed: () async 
                  isLoading = true;
                  if (cityWeatherData == null) 
                    isLoading = true;
                   else 
                    isLoading = false;
                    Map<dynamic, dynamic> data = 
                      'cityWeatherData': cityWeatherData,
                      'cityName': cityName
                    ;
                    Navigator.pop(context, data);
                  
                ,
                child: Text(
                  'Get Weather',
                ),
              ),
              Expanded(
                child: ListView.builder(
                  itemCount: cities.length,
                  itemBuilder: (context, index) 
                    return cityName.isEmpty
                        ? Container()
                        : cities[index]
                                .city
                                .toLowerCase()
                                .contains(cityName.toLowerCase())
                            ? ListTile(
                                title: Text(cities[index].city),
                                onTap: () async 
                                  setState(
                                    () 
                                      _controller.text = cities[index].city;
                                      cityName = _controller.text;

                                      print(cityName);

                                      cityLong = cities[index].long;
                                      cityLat = cities[index].lat;
                                    ,
                                  );
                                ,
                              )
                            : Container();
                  ,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  


class City 
  final String city;
  final double long;

  final double lat;
  City(this.city = '', this.lat = 0, this.long = 0);

【讨论】:

他在问题中分享了他的代码。或者@Steve Noose 在此处创建飞镖板:dartpad.dev/62deeb2566ace6a783381c081a383654

以上是关于Flutter web ListView.builder啥也没显示的主要内容,如果未能解决你的问题,请参考以下文章

flutter项目中添加web支持

如何将 Flutter 移动应用迁移到 Flutter Web

flutter Web端支持内嵌加载网页

Flutter web 最新进展: 发掘更多可能!

Flutter 调味移动应用和 Flutter web

flutter web开发环境配置