Home > Blockchain >  I am trying to use load more package
I am trying to use load more package

Time:09-12

I am trying to use load more package but when I add list and error appears because Topics isn't an int it's a class that contains int and string. I am trying this example, an error appears in list.addAll(List.generate(30, (v) => V));

in the second v: The return type 'int' isn't a 'Topics', as required by the closure's context.

and the Topics is a class here is the code :

`

class Topics {
  final int id;
  final String title;
  

  Topics({
    required this.id,
    required this.title,
   
  });

  factory Topics.fromJson(Map<String, dynamic> json) {
    return Topics(
      id: json['id'] as int,
      title: json['title'] as String,
    );
  }
}

`

`

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int get count => list.length;

  List<Topics> list = [];

  void initState() {
    super.initState();
    list.addAll(List.generate(30, (v) => v));
  }

  void load() {
    print("load");
    setState(() {
      list.addAll(List.generate(15, (v) => v));
      print("data count = ${list.length}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: Container(
        child: RefreshIndicator(
          child: LoadMore(
            isFinish: count >= 60,
            onl oadMore: _loadMore,
            child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  child: Text(list[index].toString()),
                  height: 40.0,
                  alignment: Alignment.center,
                );
              },
              itemCount: count,
            ),
            whenEmptyLoad: false,
            delegate: DefaultLoadMoreDelegate(),
            textBuilder: DefaultLoadMoreTextBuilder.chinese,
          ),
          onRefresh: _refresh,
        ),
      ),
    );
  }

  Future<bool> _loadMore() async {
    print("onLoadMore");
    await Future.delayed(Duration(seconds: 0, milliseconds: 2000));
    load();
    return true;
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 0, milliseconds: 2000));
    list.clear();
    load();
  }
}

`

Hello, I am trying this example, but nothing is shown on the screen of the emulator, any help??

CodePudding user response:

In both list.addAll(List.generate(30, (v) => v)); do it like this:

list.addAll(List.generate(30, (v) => Topics(id: v, title: 'Title $v')));

CodePudding user response:

The error occurs because you are trying to add int into a list of Topic. This line:

List.generate(30, (v) => v);

generates a list of numbers because you are mapping v (which is an int) to itself. You need to map it to a Topic like this:

list.addAll(List.generate(30, (int v) => Topic(id: v, title: "Topic $v")));

I strongly recommend to use strong typing like above with int v. You could have avoided this error by using strong typing.

CodePudding user response:

In your both initState() and load()do this:

void initState() {
    super.initState();
    list.addAll(List.generate(30, (v) => Topics(id: v, title: '$v')));
  }

  void load() {
    print("load");
    setState(() {
      list.addAll(List.generate(15, (v) => Topics(id: v, title: '$v')));
      print("data count = ${list.length}");
    });
  }

then in your container do this:

Container(
   child: Text(list[index].title),
   height: 40.0,
   alignment: Alignment.center,
)
  • Related