Home > Net >  type 'int' is not a subtype of type 'String' with Lazy Loading ListView flutter
type 'int' is not a subtype of type 'String' with Lazy Loading ListView flutter

Time:08-11

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

CodePudding user response:

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']}"),

CodePudding user response:

Can you try replace this line:

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

By this one:

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