【问题标题】:flutter nosuchmethoderror class string has no instance getter 'isnegative'颤振 nosuchmethoderror 类字符串没有实例 getter 'isnegative'
【发布时间】:2021-08-13 13:37:05
【问题描述】:

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

有人可以帮忙理解为什么它没有拉 "price" 价值吗? 如果我对其进行硬编码并将值分配给 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”没有实例获取器“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 (包:intl/src/intl/number_format.dart:786:30) #2 NumberFormat.format (package:intl/src/intl/number_format.dart:430:10) #3 AngeboteCard.build(包:hotz/screens/home/components/angebote.dart:107:80) #4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4569:28) ...

【问题讨论】:

  • 我认为问题出在 .format('$price')。只放变量,.format(price)
  • Hola Jorge,感谢您的回复。我试过了,结果是一样的...... NoSuchMethodError: The getter 'isNegative' was called on null.
  • 如果我将 NumberFormat.currency(locale: 'de-ch', decimalDigits: 2).format(price), 替换为 'CHF ''$price ', 它给了我一个半想要的结果 CHF 29.9 但它也删除了 0...
  • 你不能在那里传递一个字符串。 format 函数以 number 作为参数。
  • 我试过这种方式,它使用 2 位数字:NumberFormat.currency(locale: 'de-ch')

标签: flutter currency number-formatting


【解决方案1】:

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

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

【讨论】:

  • 嗨,Jorge,对我来说,它一直以同样的悲惨错误失败。
  • 太奇怪了,你确定是这行的问题吗? NumberFormat.currency(locale: 'de_CH').format(price),
  • 正确陈述豪尔赫。我还有三张AngeboteCard,我没有为价格增加价值,这就是它抛出错误的原因。一旦剩余的对象补充了缺少的 price: ,它就可以正常工作了。感谢您的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 2021-07-10
  • 1970-01-01
  • 2021-09-08
相关资源
最近更新 更多