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

时间:2021-05-25 10:01:16

标签: flutter currency number-formatting

我正在学习一个教程,试图创建一个水平 SingleChildScrollView,其中包含带有产品描述的对象和带有两位小数的 货币 + 价格标签 . 实现 NumberFormat.currency 方法返回错误:NoSuchMethodError class 'String' has no instance getter 'isNegative'

有人可以帮助理解为什么它没有拉动“价格”价值吗? 如果我对其进行硬编码并为 final double price = ie: 29.90; 分配一个值,则在 AngeboteCard() 类中,它会但不是来自 Angebote() 类。

这是我的课:

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),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

错误:

======== 小部件库捕获的异常 ================================== ====================== 在构建 AngeboteCard(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#ff95d], _InheritedTheme]) 时抛出了以下 NoSuchMethodError: 'String' 类没有实例 getter 'isNegative'。 接收器:“空” 尝试调用:isNegative

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

1 个答案:

答案 0 :(得分:0)

所以你不能在 .format 函数中使用 String 你需要使用数字(double 或 int),我在我的代码上尝试过这种方式并使用 2 位数正常工作:

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