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});

}

5 . Vertical viewport was given unbounded height.

Viewports expand in the scrolling direction to fill their container. In this case, a vertical viewport was given an unlimited amount of vertical space in which to expand. This situation typically happens when a scrollable widget is nested inside another scrollable widget.

If this widget is always nested in a scrollable widget there is no need to use a viewport because there will always be enough vertical space for the children. In this case, consider using a Column instead. Otherwise, consider using the “shrinkWrap” property (or a ShrinkWrappingViewport) to size the height of the viewport to the sum of the heights of its children.

There are 4 way to solve this error:-

  • Adding this two line in your ListView.builder
ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
...

if your are using GridView.builder then you can you like this-

GridView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
...
  • Wrap ListView in Expanded

Column(
  children: <Widget>[
    Expanded( // wrap in Expanded
      child: ListView(...),
    ),
  ],
)
  • Use shrinkWrap: true in ListView.
Column(
  children: <Widget>[
    ListView(
      shrinkWrap: true, // use this
    ),
  ],
)
  • Wrap ListView in SizedBox and give a bounded height
Column(
  children: <Widget>[
    SizedBox(
      height: 400, // fixed height
      child: ListView(...),
    ),
  ],
)

7 replies on “Flutter Common Errors”

Leave a Reply

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