'The method 'get' isn't defined for the type 'AppState'. pics with Flutter
Hello everyone.
I was studying "Dart and Flutter: The Complete Developer's Guide" on Udemy.
And this error showed up, I have no idea how to fix this error. Everyone on the Q&A says this course isn't updated, so if any of u guys know. Please help me fix this error.
// Import flutter helper library
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'models/image_models.dart';
import 'dart:convert';
import 'widgets/image_list.dart';
class App extends StatefulWidget {
createState() {
return AppState();
}
}
class AppState extends State<App> {
int counter = 0;
List<ImageModel> images = [];
void fetchImage() async {
counter++;
var response =
await get('https://jsonplaceholder.typicode.com/photos/$counter');
var imageModel = ImageModel.fromJson(json.decode(response.body));
setState(() {
images.add(imageModel);
});
}
// Must define a 'build' method that returns the widget that 'this' widget will show
Widget build(context) {
MaterialApp(
home: Scaffold(
body: ImageList(images),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.switch_access_shortcut_add),
onPressed: fetchImage,
),
appBar: AppBar(
title: Text('Lets see some People!'),
),
),
);
}
}
Error 1 : The method 'get' isn't defined for the type 'AppState'. Try correcting the name to the name of an existing method, or defining a method named 'get'.
Error 2 : The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.
Here's the full code , you can access it from my repository Here
Thankyou and have a great day!
Solution 1:[1]
To start, you will need to add a "return" to your widget's build method:
Widget build(context) {
return MaterialApp( // Add return here
home: Scaffold(
body: ImageList(images),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.switch_access_shortcut_add),
onPressed: fetchImage,
),
appBar: AppBar(
title: Text('Expletive'),
),
),
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Joe Muller |