1

I want to build a ListView with data from my website. I start my http request with a Void init state (if there is a better way pls tell me) And i request the array out of it in my Listview but the Listview doesnt recognice the array variable.

Does someone know how i have to transform this code so that i will get my array shown as a ListView?

Future senddata() async {
final response = await http.post(
  "https://www.bumsbirne.php", body: {
"status": "0",
});

var datauser = json.decode(response.body);
print(datauser);

return datauser;

}


class _MyHomePageState extends State<MyHomePage> {

@override
void initState() {
senddata();
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Jo wenn das klappt"),
  ),
  body: ListView.builder(
      padding: const EdgeInsets.all(16.0),
      itemCount: datauser == null ? 0 : datauser.length,
      itemBuilder: (context, index) {
        return _buildImageColumn(datauser[index]);
        // return _buildRow(data[index]);
      })
 );
 }
}

This is the Error: Error: The getter 'datauser' isn't defined for the class '_MyHomePageState'.

Update:

After using the answer i had this Problem: Type 'String' is not a subtype of type 'int' of 'index' Because of the formation of my json however i could fix it by using the method of this thread: Type 'String' is not a subtype of type 'int' of 'index'

Charles Down
  • 427
  • 1
  • 6
  • 15

2 Answers2

2

You can copy paste run full code below
Step 1: You can use Future<dynamic> senddata()
Step 2: declare dynamic datauser; and in initState call another function to do async await

class _MyHomePageState extends State<MyHomePage> {
  dynamic datauser;

  callSendData() async {
    datauser = await senddata();
    setState(() {});
  }

  @override
  void initState() {
    callSendData();
  }

working demo

enter image description here

full code

import 'dart:convert';

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

Future<dynamic> senddata() async {
  /* final response = await http.post(
      "https://www.bumsbirne.php", body: {
    "status": "0",
  });

  var datauser = json.decode(response.body);*/
  await Future.delayed(Duration(seconds: 3), () {});
  String jsonString = '''
 [
   {
     "url" : "abc"
   },
   {
     "url" : "def"
   }
 ]
 ''';
  http.Response response = http.Response(jsonString, 200);
  dynamic datauser = json.decode(response.body);
  print(datauser);

  return datauser;
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  dynamic datauser;

  callSendData() async {
    datauser = await senddata();
    setState(() {});
  }

  @override
  void initState() {
    callSendData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Jo wenn das klappt"),
        ),
        body: datauser == null
            ? Center(child: CircularProgressIndicator())
            : ListView.builder(
                padding: const EdgeInsets.all(16.0),
                itemCount: datauser == null ? 0 : datauser.length,
                itemBuilder: (context, index) {
                  return Text(datauser[index]["url"]);
                  // return _buildRow(data[index]);
                }));
  }
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
  • OMG. I don't know how to Thank you. It works perfectly. Big thanks from Germany. You gave me the Power to delete 20< Taps. – Charles Down Oct 29 '20 at 14:34
0

one of many solution is define senddata method inside _MyHomePageState this class