Home > Enterprise >  type 'String' is not a subtype of type 'int' in type cast from mapped firestored
type 'String' is not a subtype of type 'int' in type cast from mapped firestored

Time:07-03

so i am very new to flutter so please forgive me in advance for any scrappy code.. okay so i am trying to grab data stored in firebase as a map in the below function called getTotalBalance and then use some of the details in calculating the total balance, total expense and totalIncome and for some reason i keep getting an error saying

type 'String' is not a subtype of type 'int' in type cast.

class Calculations {
  static int totalBalance = 0;
  static int totalIncome = 0;
  static int totalExpense = 0;

  static getTotalBalance(Map entireData) {
    int totalBalance = 0;
    int totalIncome = 0;
    int totalExpense = 0;

  

      entireData.forEach((key, value) async {
      print(value);

      if (value['Income'] == 'Income') {
        totalBalance  = (value['amount'] as int);
        totalIncome  = (value['amount'] as int);
      } else {
        totalBalance -= (value['amount'] as int);
        totalExpense  = (value['amount'] as int);
      }
    });
  }
}

   
 Center(
              child: Padding(
                padding: const EdgeInsets.only(left: 20, right: 20),
                child: StreamBuilder<QuerySnapshot>(
                    stream: _firebaseFirestore
                        .where('userId', isEqualTo: user?.uid)
                        .snapshots(),
                    builder: (context, AsyncSnapshot snapshot) {
                      if (snapshot.hasError) {
                        // TODO: add a snackbar
                      }
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return const Center(
                          child: CircularProgressIndicator(),
                        );
                      }
                      final List storedocs = [];
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                        Map a = document.data() as Map<String, dynamic>;
                        storedocs.add(a);
                        a['id'] = document.id;
                      }).toList();
                      Calculations.getTotalBalance(storedocs.asMap());
                      return ListView.builder(
                          shrinkWrap: true,
                          itemCount: storedocs.length,
                          itemBuilder: (context, index) {
                            TaskModel task =
                                TaskModel.fromJson(snapshot.data.docs[index]);
                            return Column(
                              children: [
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Row(
                                      children: [
                                        Container(
                                          width: 50,
                                          height: 50,
                                          decoration: const BoxDecoration(
                                              color: Colors.orange,
                                              shape: BoxShape.circle),
                                          child: const Center(
                                            child: Icon(Icons.person),
                                          ),
                                        ),
                                        const SizedBox(
                                          width: 15,
                                        ),
                                        Container(
                                          height: 40,
                                          width: 90,
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                task.item,
                                                style: const TextStyle(
                                                    fontWeight: FontWeight.bold,
                                                    fontSize: 15,
                                                    color: Colors.white),
                                              ),
                                              const SizedBox(
                                                height: 5,
                                              ),
                                              Text(
                                                task.date,
                                                style: const TextStyle(
                                                    fontWeight: FontWeight.w400,
                                                    fontSize: 12,
                                                    color: Colors.grey),
                                              ),
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        Text(
                                          task.amount,
                                          style: const TextStyle(
                                              fontSize: 15,
                                              fontWeight: FontWeight.w600,
                                              color: Colors.green),
                                        )
                                      ],
                                    ),
                                    IconButton(
                                        icon: const Icon(
                                          Icons.close,
                                          color: Colors.orange,
                                        ),
                                        onPressed: () {
                                          FirebaseFirestore.instance
                                              .collection('tasks')
                                              .doc(task.id)
                                              .delete();
                                        }),
                                  ],
                                ),
                                const Padding(
                                  padding: EdgeInsets.only(left: 65, top: 8),
                                  child: Divider(
                                    thickness: 0.8,
                                  ),
                                )
                              ],
                            );
                          });
                    }),
 E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 
    E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 
    E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 
    E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 
    E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 
    E/flutter ( 9543): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast
    E/flutter ( 9543): #0      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:18:42)
    E/flutter ( 9543): #1      Calculations.getTotalBalance.<anonymous closure> (package:daddys_app/calculations.dart:11:24)
    E/flutter ( 9543): #2      ListMapView.forEach (dart:_internal/list.dart:242:8)
    E/flutter ( 9543): #3      Calculations.getTotalBalance (package:daddys_app/calculations.dart:11:16)
    E/flutter ( 9543): #4      _BarItemPageState.build.<anonymous closure> (package:daddys_app/Screens/nav_pages/bar_item_page.dart:143:36)
    E/flutter ( 9543): #5      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
    E/flutter ( 9543): #6      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
    E/flutter ( 9543): #7      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
    E/flutter ( 9543): #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
    E/flutter ( 9543): #9      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
    E/flutter ( 9543): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
    E/flutter ( 9543): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
    E/flutter ( 9543): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
    E/flutter ( 9543): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
    E/flutter ( 9543): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    E/flutter ( 9543): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
    E/flutter ( 9543): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
    E/flutter ( 9543): #17     _rootRun (dart:async/zone.dart:1426:13)
    E/flutter ( 9543): #18     _CustomZone.run (dart:async/zone.dart:1328:19)
    E/flutter ( 9543): #19     _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
    E/flutter ( 9543): #20     _invoke (dart:ui/hooks.dart:151:10)
    E/flutter ( 9543): #21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
    E/flutter ( 9543): #22     _drawFrame (dart:ui/hooks.dart:115:31)
    E/flutter ( 9543): 

CodePudding user response:

I think issue comes from

if (value['Income'] == 'Income') {
        totalBalance  = (value['amount'] as int);
        totalIncome  = (value['amount'] as int);
      } else {
        totalBalance -= (value['amount'] as int);
        totalExpense  = (value['amount'] as int);
      }

Instead of using as int, which might be getting wrong data format value while reading map(value), you can use int.tryPerse.
Replace those code by following,

  totalBalance  = int.tryParse(value['amount']) ?? 0;
  • Related