0

I am trying to show the data from database to Flutter. I am able to get the data but don't know how can i show it in Flutter. Single data i can show but getting hard time presenting multiple data.

I am using DIO plugin for HTTP requests.

Here is the code.

Future getData() async{

    try{  
      dtguid ='34';            
      var bodyss = { "uid" : dtgUid, "deviceid": deviceid};

      Response<Map> responsess =
          await Dio().post("http://192.168.100.4:8080/sampleapp/get-followingdata.php", data: bodyss,);
         Map responseBody = response.data;
        if(responseBodys['success'] == false){

           _showSnackBar(context,responseBody['errors']['inputuid'],Colors.redAccent);

          this.setState((){
            _inProcess = false;
          });

        }else{
          print(responseBody['success']);
          totalcount = responseBody['count'];
          this.setState((){

            _inProcess = false;
          });

        }

    }catch(e){
       print("Exception Caught: $e");
    }

  } 

Here is the Widget where i need to show this data.

Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
               'Test',//  schoolLists[index]['name'],
                  style: TextStyle(
                      color: primary,
                      fontWeight: FontWeight.bold,
                      fontSize: 18),
                ),
                SizedBox(
                  height: 6,
                ),
                Row(
                  children: <Widget>[
                    Icon(
                      Icons.location_on,
                      color: secondary,
                      size: 20,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      'Earth',//schoolLists[index]['location'],
                        style: TextStyle(
                            color: primary, fontSize: 13, letterSpacing: .3)),
                  ],
                ),
                SizedBox(
                  height: 6,
                ),
                Row(
                  children: <Widget>[
                    Icon(
                      Icons.school,
                      color: secondary,
                      size: 20,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      'Some Data',
                      //schoolLists[index]['type'],
                        style: TextStyle(
                            color: primary, fontSize: 13, letterSpacing: .3)),
                  ],
                ),
              ],
            ),
          )

For testing purpose i tried with some hardcoded data schoolLists that is working but i don't know how can i show the data from http request.

Sample Data.

{"errors":[],
"content":[{"uid":34,"age":35,"name":"Test User 1","country":"India"},
{"uid":34,"age":37,"name":"Test User 2","country":"India"},
{"uid":34,"age":36,"name":"Test User 3","country":"India"}],
"success":true}

I need to show name and country to the widget. In place of this test value.

Text(
               'Test',//  schoolLists[index]['name'],
                  style: TextStyle(
                      color: primary,
                      fontWeight: FontWeight.bold,
                      fontSize: 18),
                ),

Later on i will try to work on Lazyload. Unfortunatly, i am not able to show the data so, didn't asked about the lazyload.

To be honest i am learning Flutter. I don't have much experience in it.

8
  • Are you getting some error or what..what's your issue..!? Commented May 23, 2020 at 13:30
  • Where are you callinggetData()..? Commented May 23, 2020 at 13:32
  • You can use FutureBuilder and then in the builder pass the widget you want to show. Commented May 23, 2020 at 13:38
  • @srikanth7785 Sorry for late reply. getData is being called during initstate. I can confirm i am getting the data. Just don't know how to show them in the widget. Like Name, Country etc. Commented May 23, 2020 at 14:02
  • @ShubhamGupta I am facing issue regarding passing the data to Widget. Commented May 23, 2020 at 14:03

1 Answer 1

1

i dont see you decoding the body response data on top of your code first import

import 'dart:convert' as convert;

than in your function getData() decode the server JSON response like this

//get the json data decode it and store it in decodedResponse
var decodedResponse = convert.jsonDecode(response.body);

than map thru your decodedResponse as you want

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. I think decoding is not needed as DIO plugin handled those.
can you put some code for this part. "than map thru your decodedResponse as you want" It will be helpful. I am facing issue actually in this part. voted up.
I haven't created any listview widget. Do i need to create it? If yes, how?
yes of course you need a list view to map thru the data you getting from your server i see you having only 3 objects (3 users) in your "content":[{ first Object},{ second Object},{ third object}] if you have more objects your list view will map thru every object and populate you users info
|

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.