Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve "The argument type 'String' can't be assigned to the parameter type 'int' " - Flutter

I'm trying to fetch data Online Using HTTP GET with Flutter SDK. I'm trying with this code https://github.com/RaglandCodes/Flutter-basic-API/blob/master/lib/main.dart but it is showing an error about data type that's

The argument type 'String' can't be assigned to the parameter type 'int'

Here's Error part

new Card(
     child: new Container(
     child: new Text(data[index]['name']), //error red underlying with 'name'
     padding: EdgeInsets.all(20),
     ),

Here's my main.dart

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

void main() {
  runApp(new MaterialApp(home: new HomePage(),));
}

 class HomePage extends StatefulWidget {
   @override
   _HomePageState createState() => _HomePageState();
 }

 class _HomePageState extends State<HomePage> {

  String url="https://swapi.co/api/people/";
  List<String> data;

  /*onCreate*/
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getJSONData(); //method
  }

   @override
   Widget build(BuildContext context) {
     return new Scaffold(
       appBar: AppBar(
           title: Text("my JSON app")
       ),
       body: new ListView.builder(
        // itemCount: 1,
         //itemCount: data==null ? 0 :data.length ,
         itemCount: data == null ? 0 : data.length,

         itemBuilder: (BuildContext context, int index){
           return new Container(
             child: new Center(
               child: new Column(
                 crossAxisAlignment: CrossAxisAlignment.stretch, 
                 children: <Widget>[
                   new Card(
                     child: new Container(
                       child: new Text(data[index]['name']),
                       padding: EdgeInsets.all(20),
                     ),
                   )
                 ],
               ),
             ),
           );
         },
       ),
     );
   }

   /*method*/ //RT is Future<String>
  Future<String> getJSONData() async{
    var response =await http.get(
      Uri.encodeFull(url),
      headers: {"Accept": "application/json"}
    );
   print(response.body);
   debugPrint(response.body);

    setState(() {
      var convertDataToJson= json.decode(response.body);
      data=convertDataToJson['results'];
    });

    return "Success";
  }
 }
like image 905
Irfan Akram Avatar asked May 05 '19 19:05

Irfan Akram


3 Answers

That's worked for me

http.get(Uri.https('https://swapi.co', 'api/people'));

or

http.get(Uri.parse('https://swapi.co/api/people'));
like image 174
mrKodx Avatar answered Nov 09 '22 01:11

mrKodx


You have to set data variable to List type.

That's should work:

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

void main() {
  runApp(new MaterialApp(
    home: new HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String url = "https://swapi.co/api/people/";
  List data;

  /*onCreate*/
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getJSONData(); //method
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text("my JSON app")),
      body: new ListView.builder(
        // itemCount: 1,
        //itemCount: data==null ? 0 :data.length ,
        itemCount: data == null ? 0 : data.length,

        itemBuilder: (BuildContext context, int index) {
          return new Container(
            child: new Center(
              child: new Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  new Card(
                    child: new Container(
                      child: new Text(data[index]['name'] ?? ''),
                      padding: EdgeInsets.all(20),
                    ),
                  )
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  /*method*/ //RT is Future<String>
  Future<String> getJSONData() async {
    var response = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
    print(response.body);
    debugPrint(response.body);

    setState(() {
      var convertDataToJson = json.decode(response.body);
      data = convertDataToJson['results'];
    });

    return "Success";
  }
}
like image 33
Moujabr Avatar answered Nov 09 '22 02:11

Moujabr


If you add .toString() the error will disappear:

Text(data[index]['name'].toString())

good

like image 26
Mustafa Mahmoud Avatar answered Nov 09 '22 01:11

Mustafa Mahmoud