Reputation: 27
I have some problem that I cannot solve, please help me. I want to fetch data from database, and this data is json list... I wrote some code but I could not print on the console... I will add the code below, please help me... I guess I have to use for loop but when I tried I can just fetch 1 data...
codes below here:
class DenemeParca extends StatefulWidget {
final String parcaReferans;
DenemeParca({this.parcaReferans});
@override
_DenemeParcaState createState() => _DenemeParcaState();
}
Future<List<dynamic>> getIhaleData() async {
final url =
'https://www.ekspar.com.tr/onarim/dart.php';
var response = await http.get(url);
return json.decode(response.body);
}
String _parcaReferans(dynamic user) {
for (var i = 8; i < 40; i++) {
return user['parca_adi']['$i'];
}
}
class _DenemeParcaState extends State<DenemeParca> {
Widget _buildBody() {
return FutureBuilder<List<dynamic>>(
future: getIhaleData(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Container(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () async {
print(_parcaReferans(snapshot.data[index]));
},
child: Text('Get Data'),
),
],
),
);
},
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: _buildBody(),
);
}
}
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Ekspar Demo',
theme: ThemeData(
backgroundColor: Colors.white,
),
home: DenemeParca()
),
);
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return DenemeParca();
}
}
by the way link has the json codes, thank you...
Upvotes: 0
Views: 2544
Reputation: 842
Since your 'parcaAdi' is not a JSON array so you can't loop over all the values, instead you have to select each of them one by one. To fetch all the data I first created a Model for your JSON using this website. Now to fetch your data you can just do.
Future getIhaleData() async {
final url =
'https://www.ekspar.com.tr/onarim/dart-ilan-listele.php?parca_ilan_id=111';
var response = await http.get(url);
Model model = Model.fromJson(json.decode(response.body)[0]);
// now you can access your data.
print(model.parcaAdi.s1);
print(model.parcaAdi.s2);
}
Here is the code for the model class.
class Model {
ParcaAdi parcaAdi;
Model({this.parcaAdi});
Model.fromJson(Map<String, dynamic> json) {
parcaAdi =
json['parca_adi'] != null ? ParcaAdi.fromJson(json['parca_adi']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (parcaAdi != null) {
data['parca_adi'] = parcaAdi.toJson();
}
return data;
}
}
class ParcaAdi {
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
String s7;
String s8;
ParcaAdi(
{this.s1, this.s2, this.s3, this.s4, this.s5, this.s6, this.s7, this.s8});
ParcaAdi.fromJson(Map<String, dynamic> json) {
s1 = json['1'];
s2 = json['2'];
s3 = json['3'];
s4 = json['4'];
s5 = json['5'];
s6 = json['6'];
s7 = json['7'];
s8 = json['8'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['1'] = s1;
data['2'] = s2;
data['3'] = s3;
data['4'] = s4;
data['5'] = s5;
data['6'] = s6;
data['7'] = s7;
data['8'] = s8;
return data;
}
}
Upvotes: 1