flutter nosuchmethoderror class 字符串没有实例 getter 'isnegative'

flutter nosuchmethoderror class string has no instance getter 'isnegative'

我正在学习尝试创建水平 SingleChildScrollView 的教程,其中包含带有产品描述的对象和 currency + 价格标签有两位小数。 实现NumberFormat.currency方法returns报错:NoSuchMethodErrorclass'String'没有实例getter'isNegative'

有人可以帮助理解为什么它不拉 “价格” 值吗? 如果我对其进行硬编码并为 final double price = ie: 29.90; in AngeboteCard() class 赋值但不是来自 Angebote() class.

这是我的class:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hotz/constants.dart';
import 'package:intl/intl.dart';
import 'package:intl/number_symbols.dart';

class Angebote extends StatelessWidget {
  const Angebote({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        children: <Widget>[
          AngeboteCard(
            image: "assets/images/pork_belly.png",
            product: "Schweinsbrust frisch",
            price: 29.90,
            press: () {},
          ),
        ],
      ),
    );
  }
}

class AngeboteCard extends StatelessWidget {
  const AngeboteCard({
    Key key,
    this.image,
    this.title,
    this.product,
    this.price,
    this.press,
  }) : super(key: key);
  final String image, title, product;
  final double price;
  final Function press;

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return GestureDetector(
      onTap: press,
      child: Container(
        margin: EdgeInsets.only(
          left: kDefaultPadding,
          top: kDefaultPadding / 2,
          bottom: kDefaultPadding / 0.5,
          right: kDefaultPadding,
        ),
        width: size.width * 0.8,
        height: 300,
        child: Column(
          children: <Widget>[
            Image.asset(image),
            GestureDetector(
              onTap: press,
              child: Container(
                padding: EdgeInsets.all(kDefaultPadding / 2),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(10),
                    bottomRight: Radius.circular(10),
                  ),
                  boxShadow: [
                    BoxShadow(
                      offset: Offset(0, 10),
                      blurRadius: 50,
                      color: kPrimaryColour.withOpacity(0.23),
                    ),
                  ],
                ),
                child: Row(
                  children: <Widget>[
                    RichText(
                      text: TextSpan(
                        children: [
                          TextSpan(
                              text: "$title\n".toUpperCase(),
                              style: Theme.of(context).textTheme.button),
                          TextSpan(
                            text: "$product\n".toUpperCase(),
                            style: TextStyle(
                                color: kPrimaryColour.withOpacity(0.5)),
                          ),
                        ],
                      ),
                    ),
                    Spacer(),
                    Text(
                    NumberFormat.currency(locale: 'de_CH', decimalDigits: 2).format('$price'),
//                   'CHF ''$price',
                      style: Theme.of(context)
                          .textTheme
                          .button
                          .copyWith(color: kPrimaryColour),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

错误:

======== widgets库捕获异常================================ ======================= 以下 NoSuchMethodError 被抛出构建 AngeboteCard(脏,依赖项:[MediaQuery,_LocalizationsScope-[GlobalKey#ff95d],_InheritedTheme]): Class 'String' 没有实例 getter 'isNegative'。 接收者:“空” 尝试调用:isNegative

导致错误的相关小部件是: AngeboteCard file:///Users/nebojsa.kuzmanovic/Development/FlutterProjects/hotz/lib/screens/home/components/angebote.dart:29:11 抛出异常时,这是堆栈: #0 Object.noSuchMethod (飞镖:core-patch/object_patch.飞镖:54:5) #1 NumberFormat._signPrefix(包:intl/src/intl/number_format.dart:786:30) #2 NumberFormat.format(包:intl/src/intl/number_format.dart:430:10) #3 AngeboteCard.build(包:hotz/screens/home/components/angebote.dart:107:80) #4 StatelessElement.build(包:flutter/src/widgets/framework.dart:4569:28) ...

所以你不能在 .format 函数中使用字符串,你需要使用一个数字(双精度或整数),我在我的代码上尝试了这种方式,并使用了 2 位正常数字:

Text(
   NumberFormat.currency(locale: 'de_CH').format(price),
   style: Theme.of(context)
       .textTheme
       .button
       .copyWith(color: kPrimaryColour),
   )