0

I wanna fetch data from PhpMyAdmin. But i am facing one issue. I can see in the body my data. I am sharing my source code. Thx for helping. And I am working with flutter desktop.

import 'dart:convert';

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

class WarehousePage extends StatefulWidget {
  const WarehousePage({Key key}) : super(key: key);

  @override
  _WarehousePageState createState() => _WarehousePageState();
}

class _WarehousePageState extends State<WarehousePage> {
  List data = [];

  @override
  void initState() {
    fetchData();
    super.initState();
  }

  void fetchData() async {
    final response =
        await http.get(Uri.parse('url/getdata.php'));

    data = json.decode(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: data.length,
        itemBuilder: (BuildContext context, int index) => ListTile(
          title: Text(data[index]['productName']),
        ),
      ),
    );
  }
}

Update my code but still same. What is wrong? I have not any model class. I am see my data in the body and debug console. But i cant write it in the listview.

import 'dart:convert';
import 'dart:io';

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

class WarehousePage extends StatefulWidget {
  const WarehousePage({Key key}) : super(key: key);

  @override
  _WarehousePageState createState() => _WarehousePageState();
}

class _WarehousePageState extends State<WarehousePage> {
  List data = [];

  @override
  void initState() {
    fetchData();
    super.initState();
  }

  Future<List> fetchData() async {
    var jsonResponse;
    try {
      var url = Uri.parse('https://rul/getdata.php');
      var response = await http.get(url).timeout(const Duration(seconds: 20));
      if (response.statusCode == 200) {
        jsonResponse = json.decode(response.body);
        if (jsonResponse != null) {
          return (json.decode(response.body) as List)
              .map((data) => (data))
              .toList();
        }
      }
    } on SocketException catch (e) {
      print(e);
    } catch (e) {
      print(e);
    }
    return jsonResponse;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: data.length,
        itemBuilder: (BuildContext context, int index) => ListTile(
          title: Text(data[index]['productName']),
        ),
      ),
    );
  }
}

enter image description here

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43

1 Answers1

0
static Future<List<SomeModel>> fetchQuizTest() async {
var jsonResponse;
try {
  var url = Uri.parse(url');

  var response = await http.get(url).timeout(const Duration(seconds: 20));
  if (response.statusCode == 200) {
    jsonResponse = json.decode(response.body);

    if (jsonResponse != null) {
      return (json.decode(response.body) as List)
          .map((data) => new SomeModel.fromJson(data))
          .toList();
    }
  }
} on SocketException catch (e) {
  print(e);

} catch (e) {
 
  print(e);
}
return jsonResponse;

}