Home > Net >  Flutter how to get data from api without future builder
Flutter how to get data from api without future builder

Time:10-25

I tried to get data from api like this, but I couldn't display it I need to get data from request api and show it in widget, api gives me data of one user, i want to show my data like this :

 getUser() async {
    var response = await http.get(Uri.parse(url));
    var jsonData = jsonDecode(response.body);
    return jsonData['data'].map<User>(User.fromJson).toString();
  }
Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(12))),
              padding: EdgeInsets.only(left: 10, right: 10),
              height: 120,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CircleAvatar(
                      radius: 28,
                      backgroundImage: NetworkImage('API AVATAR'),
                    ),
                  Text(
                    'TEXT FROM MY API',
                    style: TextStyle(fontSize: 17, color: Colors.black),
                  ),
                  Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey,
                  ),
                ],
              ),
            ), ```

CodePudding user response:

As a ShortCut Call Get Data Function In a SetState of Init Method, or Build But a Proper Way would be to Use Provider Package and Listen for Data In the Builder,By Implementing Change Notifier Class

CodePudding user response:

You put your function that calls your api in the initState and when it is finished you do a setState.

You need to make a loading page with generic waiting texts. You make variables that will change when your API has been called.

But the best way to do what you want to do is with the Future Builder. Why don't you want to use it?

  • Related