颤振错误:未为类"对象"定义运算符'[]'?



我有一个这样的错误:

- 'Object' is from 'dart:core'.
Try correcting the operator to an existing operator, or defining a '[]' operator.
itemCount: snapshot.data['data'].length,
^
: Error: The operator '[]' isn't defined for the class 'Object?'.
lib/screens/homepage.dart:33
- 'Object' is from 'dart:core'.
Try correcting the operator to an existing operator, or defining a '[]' operator.
return Text(snapshot.data['data'][index]['name']);
^

这是我的代码主页。

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class HomePage extends StatelessWidget {
const HomePage({
Key? key,
}) : super(key: key);
final String url = 'http://10.0.2.2:8000/api/products';
//android emulator http://10.0.2.2 mac http://127.0.0.1
Future getProducts() async {
var response = await http.get(Uri.parse(url));
print(json.decode(response.body));
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Botang Store'),
),
body: FutureBuilder(
future: getProducts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data['data'].length,
itemBuilder: (context, index) {
return Text(snapshot.data['data'][index]['name']);
});
} else {
return Text('Data Error');
}
}));
}
}

This is my main.dart:

import 'package:flutter/material.dart';
import 'package:toko_online/screens/homepage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Botang Store',
home: HomePage(),
);
}
}

这是我的pubspec.yaml

name: toko_online
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: "none" # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.13.0 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
http: ^0.13.0
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
#   - images/a_dot_burr.jpeg
#   - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
#   - family: Schyler
#     fonts:
#       - asset: fonts/Schyler-Regular.ttf
#       - asset: fonts/Schyler-Italic.ttf
#         style: italic
#   - family: Trajan Pro
#     fonts:
#       - asset: fonts/TrajanPro.ttf
#       - asset: fonts/TrajanPro_Bold.ttf
#         weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

有人能解出来吗?我到处找,但没找到。我是新的扑,所以我希望每个人都能给我的代码的答案。我只是遵循教程,但当我尝试它时,我有一个这样的错误。

谢谢之前。

分析器正在抱怨,因为它不知道snapshot.data['data']是什么类型,您需要将其分配给一个变量并将其转换为List或任何类型它实际上是(基于代码,它看起来像List<Map<String,dynamic>>)。

/ it's complaining about this.
return Text(snapshot.data['data'][index]['name']);

类似:

if (snapshot.hasData) {
final data = snapshot.data['data'] as List<Map<String, dynamic>>;
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return Text(data[index]['name']);
});
} else {
return Text('Data Error');
}

你可以考虑将其解码为有意义的Model类,而不是使用原始的map和Lists。