条件为真时不显示循环进度指示器

Circular Progress Indicator not displayed when the condition is true

    import 'package:flutter/material.dart';
    import 'package:futureprovider/Employee.dart';
    import 'package:futureprovider/EmployeeService.dart';
    import 'package:provider/provider.dart';

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

    class MyApp extends StatelessWidget {

      final PostService fur = PostService();

      @override
      Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
            FutureProvider(create:(context)=> fur.getEmployees(),
            catchError: (context,e){
              print(e.toString());
              },
              //initialData: [Post(id:1,name: 1,title: '1',body: '1')],
            )
          ],
          child: MaterialApp(
            home: Scaffold(
              appBar: AppBar(title: Text("Posts Dashboard")),
              body:TilesOfPost(),
            ),
          ),
        );
      }
    }

    class TilesOfPost extends StatelessWidget {
      @override
      Widget build(BuildContext context) {

        List<Post> posts = Provider.of<List<Post>>(context);

        print("Length:"+posts.length.toString());

        return (posts==null)? Center(child:CircularProgressIndicator()):ListView.builder(
            itemCount: posts.length,
            itemBuilder: (context,index){
              return ListTile(
                title: Text(posts[index].id.toString()),
                subtitle: Text(posts[index].name.toString())
              );
            }
        );
      }
    }

在下面的代码中, 我有 Future Provider,它提供了一些未来的价值,当我执行代码时,

最初当列表为空时,它应该显示循环进度小部件,

但它为正在构建的长度为 null 的 ListView 提供了一个例外。

当仍未收到 Future 值时如何显示 Circular Progress Inidcator

您的 print 语句 是实际问题。当数据为空时,它也会检查长度,所以我会抛出错误,因为你也看不到 CircularProgressIndicator()。