手势库捕获的异常,类型 'Null' 不是类型 'String' 的子类型 'function result'



目前我正在编写一个商店应用程序,并试图使产品卡可点击,但出现了一个错误:

Exception caught by gesture library, type 'Null' is not a subtype of type 'String' of 'function result'

这是我的产品卡(按钮(代码:

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:minimlst/components/product_card.dart';
import 'package:minimlst/constants.dart';
import 'package:minimlst/models/product.dart';
import 'package:minimlst/screens/details/details_screen.dart';
import 'package:minimlst/screens/home/components/discount_banner.dart';
import 'package:minimlst/screens/home/components/home_header.dart';
import 'package:minimlst/size_config.dart';
class Body extends StatelessWidget {
const Body({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: getProportionateScreenWidth(20)),
HomeHeader(),
SizedBox(height: getProportionateScreenWidth(30)),
DiscountBanner(),
SizedBox(height: getProportionateScreenWidth(20)),
Column(children: [
Text(
"Our Catalog",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
]),
SizedBox(height: getProportionateScreenWidth(30)),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: [
...List.generate(
demoProducts.length,
(index) => ProductCard(
product: demoProducts[index],
press: () => Navigator.pushNamed(
context, DetailsScreen.routeName)),
),
SizedBox(width: getProportionateScreenWidth(20)),
],
),
),
],
),
),
);
}
}

这是我的产品卡代码:

import 'package:flutter/material.dart';
import 'package:minimlst/models/product.dart';
import '../constants.dart';
import '../size_config.dart';
class ProductCard extends StatelessWidget {
const ProductCard({
Key? key,
this.width = 140,
this.aspectRetio = 1.02,
required this.product,
required this.press,
}) : super(key: key);
final double width, aspectRetio;
final Product product;
final GestureTapCallback press;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: press,
child: SizedBox(
width: getProportionateScreenWidth(width),
child: Padding(
padding: EdgeInsets.only(left: getProportionateScreenWidth(20)),
child: Column(
children: [
AspectRatio(
aspectRatio: aspectRetio,
child: Container(
padding: EdgeInsets.all(getProportionateScreenWidth(20)),
decoration: BoxDecoration(
color: kSecondaryColor.withOpacity(0.2),
borderRadius: BorderRadius.circular(15),
),
child: Image.asset(product.images[0])),
),
Text(
product.title,
style: TextStyle(color: Colors.black),
maxLines: 2,
),
Row(
children: [
Text(
"$${product.price}",
style: TextStyle(
fontSize: getProportionateScreenWidth(18),
fontWeight: FontWeight.w600,
color: kPrimaryColor,
),
),
],
),
],
),
),
),
);
}
}

如果可以的话,请提供帮助,如果你需要lmk 代码的任何其他部分,任何形式的帮助都将不胜感激

the issue is that you are passing wrong function argument to press
As syntax is () => Navigator.pushNamed(
context, DetailsScreen.routeName)
as fat arrow refers return so you are returning Navigator.pushNamed as argument required function who's return type must be void etc
so the change is press:(){
Navigator.pushNamed(context, DetailsScreen.routeName)
}

相关内容

最新更新