Mohd Azwan
Mohd Azwan

Reputation: 101

value from MySQL did not show up in flutter

I want to show the data that I already fetch from MySQL. and when I check the file, there is data in there but when I am trying to run the flutter nothing shows up in the system. can someone view what wrong with my code to fetch the data into flutter?

PHP code:

enter image description here

PHP file:

enter image description here

The system:

enter image description here

The flutter code:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as HTTP;

class SystemUser extends StatefulWidget {
  @override
  _SystemUserState createState() => _SystemUserState();
}

class _SystemUserState extends State<SystemUser> {

  Future<List> getUser() async{

    final response= await http.get("http://10.0.2.2/foodsystem/getuser.php");
    return json.decode(response.body);

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 450,
      child: Scaffold(
        body: new FutureBuilder<List>(
          future: getUser(),
          builder: (context, snapshot){
            if(snapshot.hasError) print(snapshot.error);

            return snapshot.hasData
                ? new UserList()
                : new Center(child: new CircularProgressIndicator(),);
          },
        ),
      ),
    );
  }
}

class UserList extends StatelessWidget {

  final List list;
  UserList({this.list});
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        color: Colors.grey,
        height: 450,
        child: new ListView.builder(
            itemCount: list==null ? 0 : list.length,
            itemBuilder: (context, i){
              return Container(
                padding: const EdgeInsets.all(10.0),
                child: GestureDetector(
                  onTap: (){},
                  child: new Card(
                    child: new ListTile(
                      title: new Text(list[i]['user_email']),
                      leading: new Icon(Icons.widgets),
                      subtitle: new Text("Level : ${list[i]['level']}"),
                    ),
                  ),
                ),
              );
            },
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 106

Answers (1)

bluenile
bluenile

Reputation: 6029

You are not passing the List to UserList widget, please see and update the code as below by adding snapshot.data to UserList(snapshot.data):

....
    return snapshot.hasData
              ? new UserList(snapshot.data)
              : new Center(child: new CircularProgressIndicator(),);
    },
....

The above will work provided your getuser.php is returning proper List.

Upvotes: 2

Related Questions