How fetch data from php mysql to flutter using post request

Viewed 13

Hello as i am new to flutter, i want to fetch data from php api to flutter app with json data which have multiple data's in one file

my flutter code

Future<Album> fetchAlbum() async {
  final response = await http.post(
    Uri.parse('https://domain/folder/api.php'),
    body: jsonEncode({
      "description": 'description',
      "naw": "homeprod",
    }),
  );

  if (response.statusCode == 200) {
    // If the server did return a 201 CREATED response,
    // then parse the JSON.
    return Album.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 201 CREATED response,
    // then throw an exception.
    throw Exception('Failed to create.');
  }
}

class Album {
  //final int productid;
  String description;

  Album({this.description = ""});

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      //productid: json['product_id'],
      description: json['description'],
    );
  }
}

this my php code on server

if($postjson['naw']=="homeprod"){

     $prod = array();
   $query = mysqli_query($mysqli, "SELECT * FROM product order by rand() limit 30");
    
    while($getpost = mysqli_fetch_array($query))
   {
    $prod[] = array(
          'description' => $getpost['description']
         );
         
    
        }  
         
        if($query) $result = json_encode(array('success'=>true, 'prodlists'=>$prod));
    else $result = json_encode(array('success'=>false));

    

    echo $result;
 }

always flutter return with Null data type with sub type string.

Thank you in advance

0 Answers
Related