I keep getting an error at Line 48, Column 101 (.push(MaterialPageRoute(builder: (_) => SearchPage(HomeApp, getBooks: getBooks)));
), "The argument type 'dynamic Function()' can't be assigned to the parameter type 'HomeApp'". I recently started to learn Flutter/dart and have very little knowledge about these errors.
// 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),
),
),
)),
);
}
}
If someone can help and explain this error I would be grateful.
CodePudding user response:
The error is because you have passed the type of getbooks
parameter as HomeApp
in your SearchPage and your are passing a dynamic
function argument in it. To solve this you need to make following change to your code
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;