Home > OS >  Failed assertion: line 13 pos 16: 'map['saleVal'] != is not true
Failed assertion: line 13 pos 16: 'map['saleVal'] != is not true

Time:03-18

I'm trying to show my data ina simple bar chart ! the data is stocked on firestore . so this is my backend the full error :

The following assertion was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#50e12): 'package:login_signup/Model/sales.dart': Failed assertion: line 13 pos 16: 'map['saleVal'] != is not true.

 import 'package:cloud_firestore/cloud_firestore.dart';

class Sales{

      final int saleVal ; 
      final String saleYear ; 
      final String colorVal ; 

    Sales(this.saleVal,this.colorVal,this.saleYear);

  Sales.fromMap(Map<String, dynamic> map)
      : assert(map['saleVal'] != null),
        assert(map['saleYear'] != null),
        assert(map['colorVal'] != null),
        saleVal = map['saleVal'],
        colorVal = map['colorVal'],
        saleYear=map['saleYear'];

  @override
  String toString() => "Record<$saleVal:$saleYear:$colorVal>";
}

And this is the page for the chart where i have to show the sales via a chart bar

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'Model/sales.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'rounded_button.dart';

class ChartScreen extends StatefulWidget {
  @override
  _ChartScreenState createState() => _ChartScreenState();
}

class _ChartScreenState extends State<ChartScreen> {
     late List<charts.Series<Sales,String>> _seriesBarData; 
      late List<Sales>  myData ; 
      
      _generateData(myData){ 
        _seriesBarData = <charts.Series<Sales,String>>[]; 
        _seriesBarData.add(
          charts.Series(
            domainFn : (Sales sales,_) => sales.saleYear.toString() , 
            measureFn : (Sales sales,_) => sales.saleVal,
            colorFn :  (Sales sales,_) => charts.ColorUtil.fromDartColor(Color(int.parse(sales.colorVal))),
            id:"Sales",
            data: myData , 
            labelAccessorFn: (Sales row,_) => "${row.saleYear}"
            )
          );
        
        
      }
      @override
Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: _buildBody(context),

        
        
        );
  }
  Widget _buildBody(BuildContext context){
    return StreamBuilder<QuerySnapshot>(
       stream: FirebaseFirestore.instance.collection('sales').snapshots(),
      builder: (context,snapshot){
        children: <Widget>[
                RoundedButton(
                  colour: Colors.lightBlueAccent,
                  title: 'Dashboard',
                  onPressed: () {
                    Navigator.pushNamed(context, 'charts_screen');
                  },
                ),
              ];  
        
        if(!snapshot.hasData){
          return LinearProgressIndicator();
        }
          else{

           List<Sales> sales = snapshot.data!.docs
          .map((snapshot) => Sales.fromMap(snapshot.data() as Map<String,dynamic>))
          .toList();
          return _buildChart(context, sales);
          }
      },
      
      );
  }
  Widget  _buildChart(BuildContext context , List<Sales> saledata){
    List<Sales>  myData ; 
      myData = saledata;
      _generateData(myData){};
      return Padding(
      padding: EdgeInsets.all(8.0),
      child: Container(
        child: Center(
          child:Column(
            children: <Widget>[
              Text ('Sales by Year',
              style:TextStyle(fontSize:24.0 , fontWeight: FontWeight.bold),
              ),
              SizedBox(height : 10.0, ),
              /*Expanded(
                child: charts.BarChart(_seriesBarData,
              animate : true, 
              animationDuration: Duration(seconds:5),
              behaviors : [
                new charts.DatumLegend(
                  entryTextStyle : charts.TextStyleSpec(color: charts.MaterialPalette.purple.shadeDefault,
                  fontFamily: 'Google',
                  fontSize:18),
                )
              ],
              ),
              ),*/
            ],
          ),
          ),
      ),
    );
  }
}

CodePudding user response:

Your error occures on Sales.fromMap on the assert(map['saleVal'] != null) line. So, your map['saleVal'] happens to be null. Maybe you should drop assert lines and just use null check with default values in case of null value. Like:

Sales.fromMap(Map<String, dynamic> map)
      : saleVal = map['saleVal'] ?? 0, // default value in case of null
        colorVal = map['colorVal'] ?? '',
        saleYear = map['saleYear'] ?? '';

Or maybe make your properties nullable, like:

final int? saleVal;
final String? saleYear;
final String? colorVal;
  • Related