iampawan / PokemonApp

Pokemon App with animations and beautiful UI
283 stars 126 forks source link

NoSuchMethodError #3

Open iamshm opened 5 years ago

iamshm commented 5 years ago

I followed your code and Im getting this Error I/flutter (12085): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (12085): The following NoSuchMethodError was thrown building HomePage(dirty, state: _HomePageState#4f655): I/flutter (12085): The getter 'pokemon' was called on null. I/flutter (12085): Receiver: null I/flutter (12085): Tried calling: pokemon I/flutter (12085): I/flutter (12085): When the exception was thrown, this was the stack: I/flutter (12085): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5) I/flutter (12085): #1 _HomePageState.build package:poke_dex/main.dart:49 I/flutter (12085): #2 StatefulElement.build package:flutter/…/widgets/framework.dart:3825 I/flutter (12085): #3 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:3736 I/flutter (12085): #4 Element.rebuild package:flutter/…/widgets/framework.dart:3559 I/flutter (12085): #5 ComponentElement._firstBuild package:flutter/…/widgets/framework.dart:3716 I/flutter (12085): #6 StatefulElement._firstBuild

theCode main.dart: import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'pokemon.dart';

void main() => runApp(MaterialApp( title: "PokeDex", home: HomePage(), debugShowCheckedModeBanner: false, ));

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

class _HomePageState extends State {

var url ="https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json";

pokeData pkData ;

@override void initState() {

super.initState();
pullData();

}

pullData() async { var output = await http.get(url); print(output); var decodedJson = jsonDecode(output.body); pkData=pokeData.fromJson(decodedJson) ; }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("PokeDex"), backgroundColor: Colors.cyan, ), body: GridView.count( crossAxisCount: 2, children: pkData.pokemon.map((poke)=>Card()).toList() ),

    drawer: Drawer(child: FlutterLogo(),),

 floatingActionButton: FloatingActionButton(onPressed: (){ 
   print(Text("ScrollDown"));
   },child: Icon(Icons.search),
   backgroundColor: Colors.red,

  ),
  bottomNavigationBar: BottomAppBar(
     color: Colors.cyan,
     child: Container(height: 50.0,
    ),
   ),
    floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);

} }

Classy-Bear commented 5 years ago

You forget to add the CircularProgressIndicator while the app is fetching the data

body: pkData == null?
    Center(child: CircularProgressIndicator(),)
    :GridView.count...

This is a ternary operator that will show the circular indicator until 'pokemon' is null. When it fetches the data it will show the GridView

Jagmohanrai commented 5 years ago

I did the same but it is just showing the circular indicator but not loading the grid view so is the app not able to fetch the data??

Classy-Bear commented 5 years ago

Exactly, it means that the pkData is null and the pkData is not yet fetched. If it doesnt fetch the data in a given amount time is probably an HTTP ERROR.

Use this to know the status code of the HTTP request... http.Response response = await http.get("https://myJson/httpMethod/"); print(response.statusCode)

This should print in the console the status code

Remember to use the async keyword on your function when using the Future class

iampawan commented 5 years ago

Any issue with the data?

Jagmohanrai commented 5 years ago

No sir it's going smooth now

On Sat, 19 Oct, 2019, 5:52 PM Pawan Kumar, notifications@github.com wrote:

Any issue with the data?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/iampawan/PokemonApp/issues/3?email_source=notifications&email_token=AMD44LBUE3FKXRBM5ON7B7TQPL3X3A5CNFSM4HHLRGLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBXOHVA#issuecomment-544138196, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMD44LEKFM7JKQROCUCKYBLQPL3X3ANCNFSM4HHLRGLA .