0

This is my code:

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<User> _users = <User>[];
  List<User> _usersDisplay = <User>[];

  @override
  void initState() {
    super.initState();
    fetchUsers().then((value){
      _users.addAll(value);
      _usersDisplay = _users;
      print(_usersDisplay.length);
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Users List'),
      ),
      body: SafeArea(
        child: Container(
          child: ListView.builder(
            itemBuilder: (context, index) {
              if (_usersDisplay.length > 0){
                return UserTile(user: this._usersDisplay[index]);
              }else{
                return LoadingView();
              }
            },
            itemCount: _usersDisplay.length + 1,
          ),
        ),
      ),
    );
  }
}

While executing initStae() it prints _usersDisplay.length as 100 but only return LoadingView(). It not shows UserTile().

When checking _usersDisplay.length inside of build it is always 0. Why?

Also I cannot define List as this way: List<User> _users = List<User>();

2 Answers 2

2

Use Future Builder for JSON & List

FutureBuilder(
  builder: (context, snapshot) {

    // WHILE THE CALL IS BEING MADE AKA LOADING
    if (ConnectionState.active != null && !snapshot.hasData) {
      return Center(child: Text('Loading'));
    }

    // WHEN THE CALL IS DONE BUT HAPPENS TO HAVE AN ERROR
    if (ConnectionState.done != null && snapshot.hasError) {
      return Center(child: Text(snapshot.error));
    }

    // IF IT WORKS IT GOES HERE!
    return ListView.builder(
      itemCount: snapshot.data.length,
      itemBuilder: (context, index) {
        return Text(snapshot.data[index].toString());
      },
    );
  },
  future: getAllTodos(),
),
Sign up to request clarification or add additional context in comments.

2 Comments

How can I use Filtering or searching options when using Future Builder
Fetched data will be there in the Snapshot variable. You can access the snapshot as "snapshot.data" like list. Then, you can do whatever you want with that data.
1

change

fetchUsers().then((value){
      _users.addAll(value);
      _usersDisplay = _users;
      print(_usersDisplay.length);
    });

to

fetchUsers().then((value){
      _users.addAll(value);
      _usersDisplay = _users;
      print(_usersDisplay.length);
      setState(() {})
});

1 Comment

If you get data from API refer my answer here or here or here hope it's helpful to you in this answers I have used Futur and FutureBuilder for geting the data

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.