其他分享
首页 > 其他分享> > flultter listview异常type '(BuildContext, int) => dynamic' is not a subtype of type '

flultter listview异常type '(BuildContext, int) => dynamic' is not a subtype of type '

作者:互联网

type '(BuildContext, int) => dynamic' is not a subtype of type '(BuildContext, int) => Widget'

源码如下:

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

void main() {
  runApp(MaterialApp(
    title: 'Returning Data',
    home: HomePage(),
  ));
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter SnackBar'),
        ),
        body: Center(
          child: new ListViewLayout(),
        ));
  }
}

class ListViewLayout extends StatelessWidget {
  _istviewItemBuilder(BuildContext context, int index) {
    return Text(posts[index].author);
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: posts.length, itemBuilder: _istviewItemBuilder);
  }
}

原因是因为_istviewItemBuilder没写 返回值类型Widget 加上就可以

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

void main() {
  runApp(MaterialApp(
    title: 'Returning Data',
    home: HomePage(),
  ));
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter SnackBar'),
        ),
        body: Center(
          child: new ListViewLayout(),
        ));
  }
}

class ListViewLayout extends StatelessWidget {
  Widget _istviewItemBuilder(BuildContext context, int index) {
    return Text(posts[index].author);
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: posts.length, itemBuilder: _istviewItemBuilder);
  }
}

 

标签:Widget,return,int,istviewItemBuilder,BuildContext,context,type
来源: https://www.cnblogs.com/mingfeng002/p/11592774.html