Flutter Infinite Scroll类型null不是类型强制转换中字符串的子类型

yk9xbfzb  于 2023-04-22  发布在  Flutter
关注(0)|答案(1)|浏览(85)

我试图做一个无限的新闻小部件,所以我做了这个测试小部件(你可以把'NewsFeed'作为你的构建函数的一个孩子来调用它),但它是错误的错误'_CastError(类型'Null'不是类型'String'的子类型类型)',我不知道为什么。谢谢!
编辑:我添加了一个future.delayed,但我不认为它会被调用,因为我不必等待5分钟。
下面是我的代码以获取更多信息:

class NewsFeed extends StatefulWidget {
  @override
  GetNews createState() => GetNews();
}

class GetNews extends State<NewsFeed> {
  var _posts;

  bool _hasNextPage = true;
  bool _isFirstLoadRunning = false;
  bool _isLoadMoreRunning = false;

  void _loadMore() async {
    if (_hasNextPage == true &&
        _isLoadMoreRunning == false &&
        _controller.position.extentAfter < 300) {
      setState(() {
        _isLoadMoreRunning = true; // Display a progress indicator at the bottom
      }); // Increase _page by 1

      try {
        var res = [
          {
            'title': 'Skull Crushers vs. Tricep Pulldown',
            'by': 'someone',
            'intro-text':
                'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus est pellentesque elit ullamcorper dignissim. Pharetra convallis posuere morbi leo urna. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Ornare arcu dui vivamus arcu felis bibendum. Arcu felis bibendum ut tristique et.',
            'image':
                'https://media.self.com/photos/6398b36c72eb56f726777d06/4:3/w_2560%2Cc_limit/weekly-workout-schedule.jpeg'
          },
          {
            'title': 'Skull Crushers vs. Tricep Pulldown',
            'by': 'someone',
            'intro-text':
                'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus est pellentesque elit ullamcorper dignissim. Pharetra convallis posuere morbi leo urna. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Ornare arcu dui vivamus arcu felis bibendum. Arcu felis bibendum ut tristique et.',
            'image':
                'https://media.self.com/photos/6398b36c72eb56f726777d06/4:3/w_2560%2Cc_limit/weekly-workout-schedule.jpeg'
          }
        ];
        if (res.isNotEmpty) {
          setState(() {
            _posts.addAll(res);
          });
        } else {
          setState(() {
            _hasNextPage = false;
          });
        }
      } catch (err) {
        print('Something went wrong!');
      }

      setState(() {
        _isLoadMoreRunning = false;
      });
    }
  }

  void start() {
    _posts = [
      {
        'title': 'Skull Crushers vs. Tricep Pulldown',
        'by': 'someone',
        'intro-text':
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus est pellentesque elit ullamcorper dignissim. Pharetra convallis posuere morbi leo urna. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Ornare arcu dui vivamus arcu felis bibendum. Arcu felis bibendum ut tristique et.',
        'image':
            'https://media.self.com/photos/6398b36c72eb56f726777d06/4:3/w_2560%2Cc_limit/weekly-workout-schedule.jpeg'
      },
      {
        'title': 'Skull Crushers vs. Tricep Pulldown',
        'by': 'someone',
        'intro-text':
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus est pellentesque elit ullamcorper dignissim. Pharetra convallis posuere morbi leo urna. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Ornare arcu dui vivamus arcu felis bibendum. Arcu felis bibendum ut tristique et.',
        'image':
            'https://media.self.com/photos/6398b36c72eb56f726777d06/4:3/w_2560%2Cc_limit/weekly-workout-schedule.jpeg'
      }
    ];
    Future.delayed(Duration(seconds: 300));

    setState(() {
      _isFirstLoadRunning = false;
    });
  }

  late ScrollController _controller;
  @override
  void initState() {
    super.initState();
    _isFirstLoadRunning = true;
    start();
    _controller = ScrollController()..addListener(_loadMore);
  }

  @override
  Widget build(BuildContext context) {
    return _isFirstLoadRunning
        ? const Center(
            child: CircularProgressIndicator(),
          )
        : Column(
            children: [
              Expanded(
                child: ListView.builder(
                  itemCount: _posts.length,
                  controller: _controller,
                  itemBuilder: (_, index) => Card(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
                    child: ListTile(
                      title: Text(_posts[index]['title'] as String),
                      subtitle: Text(_posts[index]['body'] as String),
                    ),
                  ),
                ),
              ),
              if (_isLoadMoreRunning == true)
                const Padding(
                  padding: EdgeInsets.only(top: 10, bottom: 40),
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                ),
              if (_hasNextPage == false)
                Container(
                  padding: const EdgeInsets.only(top: 30, bottom: 40),
                  color: Colors.amber,
                  child: const Center(
                    child: Text('You have fetched all of the content'),
                  ),
                ),
            ],
          );
  }
}
y3bcpkx1

y3bcpkx11#

在你的帖子里没有值与关键'身体'也许试试这个

subtitle: Text((_posts[index]['body'] as String?)??""),

或者您正在尝试显示介绍文本

subtitle: Text((_posts[index]['intro-text'] as String)),

相关问题