Search code examples
fluttercurrencynumber-formatting

flutter nosuchmethoderror class string has no instance getter 'isnegative'


I am following a tutorial trying to create a horizontal SingleChildScrollView which contains object with product description and a currency + price tag with two decimals. Implementing NumberFormat.currency method returns an error: NoSuchMethodError class 'String' has no instance getter 'isNegative'

Can someone help to understand why it is not pulling "price" value please? If I hard code it and assign a value to the final double price = ie: 29.90; in AngeboteCard() class it does but not from the Angebote() class.

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

The error:

======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building AngeboteCard(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#ff95d], _InheritedTheme]): Class 'String' has no instance getter 'isNegative'. Receiver: "null" Tried calling: isNegative

The relevant error-causing widget was: AngeboteCard file:///Users/nebojsa.kuzmanovic/Development/FlutterProjects/hotz/lib/screens/home/components/angebote.dart:29:11 When the exception was thrown, this was the stack: #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) ...


Solution

  • So you cannot use String in the .format function you need to use a number (double or int), i tried this way on my code and worked with 2 digits normal:

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