0

I make list view to show data of user from Api and I make to this list Lazy Loading try to cut list each 10 user. But at first 10 user the list view work fine but when I try to move after 10 user I get this message:

type 'int' is not a subtype of type 'String'

I try to add toString like this:

ListTile(title: Text((users[index]['name'].toString()))) ;

But when I add toString All List load at first time with out make Lazy Loading.

My Code:

class HomeState extends State<Home> {
  static int page = 0;
  ScrollController _sc = new ScrollController();
  bool isLoading = false;
  List users = [];

  @override
  void initState() {
    this._getMoreData();
    super.initState();
    _sc.addListener(() {
      if (_sc.position.pixels == _sc.position.maxScrollExtent) {
        _getMoreData();
      }
    });
  }

  @override
  void dispose() {
    _sc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Lazy Load Large List"),
        ),
        body: Container(
          child: _buildList(),
        ),
        resizeToAvoidBottomInset: false,
      ),
    );
  }


  Widget _buildList() {
    return ListView.builder(
      itemCount: users.length + 1,

      padding: EdgeInsets.symmetric(vertical: 8.0),
      itemBuilder: (BuildContext context, int index) {
        if (index == users.length) {
          return _buildProgressIndicator();
        } else {
          return ListTile(title: Text((users[index]['name']))) ;

        }
      },
      controller: _sc,
    );
  }


  final int _limit = 15;
  Future _getMoreData() async {
    if (!isLoading) {
      setState(() {
        isLoading = true;
      });

      final String url = "*********************.php?=" + "&results=$_limit";
      final response = await http.get(Uri.parse(url));

      print(url);

      List tList = [];
      var responsebody = jsonDecode(response.body);
      for (int i = 0; i < responsebody.length; i++) {
        tList.add(responsebody[i]);
        print(tList);

      }
      setState(() {
        isLoading = false;
        users.addAll(tList);
        page++;
      });
    }
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Center(
        child: new Opacity(
          opacity: isLoading ? 1.0 : 00,
          child: new CircularProgressIndicator(),
        ),
      ),
    );
  }
}

enter image description here

How can I solve this problem? I don't know what is the error

thank you

13
  • Do you have the indication on which line this occur? Check the error stack and indicate the line concerned. Commented Aug 10, 2022 at 19:02
  • You have a print(tList); which should be replaced with print(tList.toList()); Commented Aug 10, 2022 at 19:03
  • Hi bro @Alaindeseine I was try make it like ( users.addAll(tList.toList());) but still problem is not solved Commented Aug 10, 2022 at 19:50
  • Yes pro the problem in this line :ListTile(title: Text(users[index]['name'])) ; @Alaindeseine Commented Aug 10, 2022 at 19:51
  • Can you provide users data snippet? Commented Aug 10, 2022 at 19:57

2 Answers 2

2

Not sure about the type of Users. Its mentioned just as a List. You can try

List<Map<String, dynamic>> users = [];

to first make sure that its a list of map to access the ['name'] from it. if it shows List<dynamic> then try

Text("${json.decode(users[index])['name']}"),
Sign up to request clarification or add additional context in comments.

4 Comments

Hi pro / I try it but still problem not solved
Type of Users string + same times int / you know like username ali6353 and same times ali just
that just example
I updated the question and uploaded a picture of the problem
1

Can you try replace this line:

final String url = "*********************.php?=" + "&results=$_limit";

By this one:

final String url = "*********************.php?=&results=" + _limit.toString();

4 Comments

Hi bro / I try it not solved problem
@MAl question removed!
Hello brother, I have updated and modified it, see here please stackoverflow.com/questions/73316743/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.