Trying to do a shpping cart project in flutter error is : The method '+' can't be unconditionally invoked because the receiver can be 'null' - TagMerge
2Trying to do a shpping cart project in flutter error is : The method '+' can't be unconditionally invoked because the receiver can be 'null'Trying to do a shpping cart project in flutter error is : The method '+' can't be unconditionally invoked because the receiver can be 'null'

Trying to do a shpping cart project in flutter error is : The method '+' can't be unconditionally invoked because the receiver can be 'null'

Asked 1 years ago
3
2 answers

Your compiler does not magically know that the return value of containsKey means that the indexer will return a non-null value. So you need to find a way to tell your compiler what you are doing, while staying with the functions and return types provided and without asking your compiler to assume things going on behind the scenes.

This should work:

void addToCart(int index) {
    final currentValue = _cart[index] ?? 0;
      
    _cart[index] = currentValue + 1;
    
    notifyListeners();
  }

You could also just use the function provided for this case:

void addToCart(int index) {
  _cart.update(index, (value) => value + 1, ifAbsent: () => 1);
  notifyListeners();
}

Source: link

0

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
    if (kReleaseMode)
      exit(1);
  };
  runApp(MyApp());
}

// rest of `flutter create` code...
class MyApp extends StatelessWidget {
...
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...
      builder: (BuildContext context, Widget widget) {
        Widget error = Text('...rendering error...');
        if (widget is Scaffold || widget is Navigator)
          error = Scaffold(body: Center(child: error));
        ErrorWidget.builder = (FlutterErrorDetails errorDetails) => error;
        return widget;
      },
    );
  }
}
OutlinedButton(
  child: Text('Click me!'),
  onPressed: () async {
    final channel = const MethodChannel('crashy-custom-channel');
    await channel.invokeMethod('blah');
  },
),
import 'dart:async';

void main() {
  runZonedGuarded(() {
    runApp(MyApp());
  }, (Object error, StackTrace stack) {
    myBackend.sendError(error, stack);
  });
}
runZonedGuarded(() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

Source: link

Recent Questions on flutter

    Programming Languages