Dart Flutter

Flutter Common Errors

1 The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’.

Old CodeReplace With
http.get(String)http.get(Uri.parse(String))
http.post(String)http.post(Uri.parse(String))
and So On

In your specific example, you will need to use Like This :-

await http.post(
  Uri.parse("https://api.instagram.com/oauth/access_token"),
  body: {
    "client_id": clientID,
    "redirect_uri": redirectUri,
    "client_secret": appSecret,
    "code": authorizationCode,
    "grant_type": "authorization_code",
  });

2. The operator ‘[]’ isn’t defined for the type ‘Object’.

ModalRoute.settings.arguments is a property with the type Object.so you can make it Map

  Widget build(BuildContext context) {
    var rcvdData = ModalRoute.of(context)!.settings.arguments as Map;
    print(rcvdData['name']);
    print("rcvd data  is : ${rcvdData}");

3 . String Convert Into Double

String numInString = '3467.19'; 

double  num1= double.parse('${numInString}');

4. A value of type ‘Object?’ can’t be assigned to a variable of type ‘Map <dynamic,dynamic>’.

create a class for define the argument. what you have needed.

class ProductDetailsArguments {
  final Product product;

  ProductDetailsArguments({required this.product});

}

Now extracts the arguments Like This :-

 Widget build(BuildContext context) {
    final args = ModalRoute.of(context)!.settings.arguments as ProductDetailsArguments;

Full Code

You can Send Argument like This

 onTap: (){
                Navigator.pushNamed(
                context,
                DetailsScreen.routeName,
                arguments: ProductDetailsArguments(product: product),
                );
           },

Now get extracts arguments

import 'package:flutter/material.dart';

import '../../models/Products.dart';
import 'components/body.dart';
import 'components/custom_app_bar.dart';

class DetailsScreen extends StatelessWidget {
  static String routeName = "/details";


  // late Map product;
  @override
  Widget build(BuildContext context) {
    final args = ModalRoute
        .of(context)!
        .settings
        .arguments as ProductDetailsArguments;
    return Scaffold(
      backgroundColor: Color(0xFFF5F6F9),
      // appBar:AppBar(title: Text("hii"),),
      appBar: PreferredSize(
          preferredSize:Size.fromHeight(AppBar().preferredSize.height),
          child: CustomAppBar()
      ),
      body: Body(product: args.product, key: ValueKey("singleProduct"),),
      // body:Container(),
    );
  }
}




class ProductDetailsArguments {
  final Product product;

  ProductDetailsArguments({required this.product});

}

Ask Question

2 replies on “Flutter Common Errors”

Leave a Reply

Your email address will not be published. Required fields are marked *