当前位置: 首页 > 知识库问答 >
问题:

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

臧友樵
2023-03-14

我正在遵循一个教程,试图创建一个水平的SingleChildScrollView,它包含带有产品描述的对象和带有两个小数的货币+价格标签。实现NumberFormat.Currency方法返回一个错误:NosuchMethodeRorr类“String”没有实例getter“is negal”

有人能帮助理解为什么它不拉“价格”价值吗?如果我对其进行硬编码,并为最终的双倍价格赋值=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),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

widgets库捕获的异常接收者:“null”尝试调用:isNegative

相关的导致错误的小部件是:AngeboteCard file:///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/components/angebote.dart:107:80)#4 statelesselement.build(包:flutter/src/widgets/framework.dart:4569:28)...

共有1个答案

太叔繁
2023-03-14

所以你不能在。format函数中使用字符串,你需要使用一个数字(double或int),我在代码中尝试了这种方法,并使用了正常的2位数:

Text(
   NumberFormat.currency(locale: 'de_CH').format(price),
   style: Theme.of(context)
       .textTheme
       .button
       .copyWith(color: kPrimaryColour),
   )
 类似资料:
  • 我是从火神那里收取数据的,我有这个,我是火神的新手,我知道的不多,所以你能帮我吗? 错误是这样的:类'AsyncSnapshot'没有实例getter'docs'。接收器:'AsyncSnapshot'的实例尝试调用:docs

  • 我的代码中有两个流,第一个是从朋友列表中获取用户ID,第二个流是使用ID列表在Firebase中搜索用户ID的文档。 这是火力恢复代码。 我不知道为什么会发生这种情况,因为我可以显示id列表。我曾尝试将更改为,但也会产生相同的错误。 编辑:

  • 我有一个应用程序,我想从Firestore数据库中检索uid文档中表示的消息,如下所述,这些消息存储如下:聊天室- 但我收到以下错误: 生成StreamBuilder时引发了以下NoSuchMethodError(脏,状态:\u StreamBuilderBaseState 导致错误的相关小部件是:StreamBuilderfile:///Users/ahmedhussain/Downloads/

  • 你能帮我解决这个问题吗?我试图从存储在sqflite中的数据返回一个FutureBuilder,返回它并用它构建一个小部件列表,但我遇到了一个小问题,我不知道如何以正确的方式返回列表: 我得到的错误是: 下面是我的消息类: 我是这样称呼它的。我的问题在于我调用并试图使用快照数据的地方。然后我实例化BeautifulBubble类,我认为这是可以的。 同时添加dbhelper.getmessages

  • 问题内容: 我有两个课,和。我得到的字符串可以是或。 而不是使用长语句,例如: 有没有一种方法可以使用字符串创建实例,即? 问题答案: 是的你可以! 使用名称空间时,请提供标准名称: 您可以在php中执行的其他一些很酷的工作是: 以及可变的函数和方法。

  • 我最近开始使用Firebase fiRecovery,这是一个我似乎找不到答案的错误,我在查看类似问题的答案后尝试将snapshot.data.documents编写为文档、文档和文档,但它仍然抛出相同的错误。我该怎么办?我正在使用的fiRecovery版本是cloud_firestore:^1.0.7 它引发的异常