我一直在第 48 行第 101 列 ( .push(MaterialPageRoute(builder: (_) => SearchPage(HomeApp, getBooks: getBooks)));)處收到錯誤訊息,“無法將引數型別‘動態函式()’分配給引數型別‘HomeApp’”。我最近開始學習 Flutter/dart,對這些錯誤知之甚少。
// ignore_for_file: void_checks
import 'package:http/http.dart' as http;
import 'package:googleapis/books/v1.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:book_search_app/widgets/book_card.dart';
import 'package:book_search_app/screens/profile.dart';
class HomeApp extends StatefulWidget {
const HomeApp({Key? key}) : super(key: key);
@override
_HomeAppState createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
getBooks() async {
var response = await http.get(Uri.parse(
'https://www.googleapis.com/books/v1/volumes?q={search}:keyes&key={api key}'));
print(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(106),
child: AppBar(
automaticallyImplyLeading: false,
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(bottom: Radius.circular(15))),
backgroundColor: const Color(0xFFC4C4C4),
title: const Text(
'Book Search',
style: TextStyle(fontSize: 24, color: Colors.black),
),
actions: [
IconButton(
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => SearchPage(HomeApp, getBooks: getBooks)));
},
icon: const Icon(Icons.search_rounded),
iconSize: 40,
color: Colors.black,
)
],
),
),
body: const BookCard(),
bottomNavigationBar: ClipRRect(
borderRadius: const BorderRadius.vertical(top: Radius.circular(15)),
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: const Color(0xFFC4C4C4),
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(
icon: Icon(Icons.category), label: 'Categories'),
BottomNavigationBarItem(
icon: Icon(Icons.favorite), label: 'Favourite'),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart), label: 'Cart'),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle_rounded),
label: 'Profile',
),
],
),
));
}
}
class SearchPage extends StatelessWidget {
// ignore: non_constant_identifier_names
SearchPage(HomeApp, {Key? key, required this.getBooks,})
: super(key: key);
final myController = TextEditingController();
final HomeApp getBooks;
@override
void dispose() {
myController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// The search area here
title: Container(
width: double.infinity,
height: 40,
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(5)),
child: Center(
child: TextField(
controller: myController,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
suffixIcon: IconButton(
icon: const Icon(Icons.search),
onPressed: () {
getBooks;
},
),
hintText: 'Search...',
border: InputBorder.none),
),
),
)),
);
}
}
如果有人可以幫助并解釋這個錯誤,我將不勝感激。
uj5u.com熱心網友回復:
錯誤是因為您傳遞了SearchPage 中的getbooks引數型別,HomeApp并且在其中傳遞了一個dynamic函式引數。要解決此問題,您需要對代碼進行以下更改
class SearchPage extends StatelessWidget {
// ignore: non_constant_identifier_names
SearchPage(HomeApp, {Key? key, required this.getBooks,})
: super(key: key);
final myController = TextEditingController();
final Function getBooks;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377661.html
