Home > Net >  The argument type 'String?' can't be assigned to the parameter type 'String'
The argument type 'String?' can't be assigned to the parameter type 'String'

Time:12-28

The argument type 'String?' can't be assigned to the parameter type 'String'.

Container(
              width: 54,
              height: 54,
              decoration:BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  image: NetworkImage(
                    user.profilePhotoUrl,
                  ),
                ),
              ),
            ),

please helpmee

CodePudding user response:

Dart offers null safety to the users.

For example when we declare String? name; default value of name is null and can be changed.

But we cannot declare String name; you have to give value while declaring like String name = 'Funny Name'; ensuring that value of name can never be null.

Here use ! mark after the variable to tell the compiler that value of user.profilePhotoUrl is not null like user.profilePhotoUrl!

My suggestion is to use String intead of String? in that case you don't need to use !

CodePudding user response:

we should always handle null value case first. Here if in case a the value of "profilePhotoUrl" is equal to null then we have handle it with this default link which is after ?? 'any other link'.

Container(
              width: 54,
              height: 54,
              decoration:BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  image: NetworkImage(
                    user.profilePhotoUrl ?? 'any other link',
                  ),
                ),
              ),
            ),

Here is an example : In below example i haven't provided any value to profilePhotoUrl and declared it as nullable & so when it execute it will get null but as we have handle it already using null operator.

?? Called also null operator. This operator returns expression on its left, except if it is null, and if so, it returns right expression:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  
  String? profilePhotoUrl;
  
  @override
  Widget build(BuildContext context) {
    return Container(
              width: 350,
              height: 450,
              decoration:BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  image: NetworkImage(
                    profilePhotoUrl ?? 'https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg',
                  ),
                ),
              ),
            );
  }
}

What are ??, ??=, ?., …? in Dart? https://jelenaaa.medium.com/what-are-in-dart-df1f11706dd6

CodePudding user response:

Add ! to user.profilePhotoUrl, like this:

user.profilePhotoUrl!,
  • Related