Why cant I use .sum in a Stateful Widget?

Why cant I use .sum in a Stateful Widget?

Problem Description:

I have built out a stateless widget that I now need to change to a stateful widget. Within my stateless widget, I have a function…

            SumOfRoundedTankValue() {
              List<double> numbers = [];
                port.ports.tankSpecs.forEach((e) {
                final sum = numbers.sum;
                return (sum);

Why does this function not work in a stateful widget? I get the squiggly line under .sum that says:

The getter ‘sum’ isn’t defined for the type ‘List’.
Try importing the library that defines ‘sum’, correcting the name to the name of an existing getter, or defining a getter or field named ‘sum’.

Solution – 1

This is not issue of Statless or Statefull widgets

Just import to your dart file

import 'package:collection/collection.dart';

After that sum property will be available to your Iterable

final sum = numbers.sum;
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.