I'm using a cached network image to load in an image from firebase and if the image is URL is null it loads a circle avatar with an icon in it.
It does not work fine in the emulator
Exception has occurred. ArgumentError (Invalid argument(s): No host specified in URI ) Here's The Code
class ProfileScreen extends StatefulWidget {
final String uid;
const ProfileScreen({
Key? key,
required this.uid,
}) : super(key: key);
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
final ProfileController profileController = Get.put(ProfileController());
@override
void initState() {
super.initState();
profileController.updateUserId(widget.uid);
}
@override
Widget build(BuildContext context) {
return GetBuilder<ProfileController>(
init: ProfileController(),
builder: (controller) {
if (controller.user.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black12,
leading: const Icon(
Icons.person_add_alt_1_outlined,
),
actions: const [
Icon(Icons.more_horiz),
],
title: Text(
controller.user['name']??"",
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ClipOval(
child:
CachedNetworkImage(
fit: BoxFit.cover,
imageUrl: controller.user['profilePhoto']??"",
height: 100,
width: 100,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(
Icons.error,
),
),
)
],
),
const SizedBox(
height: 15,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
Text(
controller.user['following'],
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 5),
const Text(
'Following',
style: TextStyle(
fontSize: 14,
),
),
],
),
Container(
color: Colors.black54,
width: 1,
height: 15,
margin: const EdgeInsets.symmetric(
horizontal: 15,
),
),
Column(
children: [
Text(
controller.user['followers'],
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 5),
const Text(
'Followers',
style: TextStyle(
fontSize: 14,
),
),
],
),
Container(
color: Colors.black54,
width: 1,
height: 15,
margin: const EdgeInsets.symmetric(
horizontal: 15,
),
),
Column(
children: [
Text(
controller.user['likes'],
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 5),
const Text(
'Likes',
style: TextStyle(
fontSize: 14,
),
),
],
),
],
),
const SizedBox(
height: 15,
),
Container(
width: 140,
height: 47,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black12,
),
),
child: Center(
child: InkWell(
onTap: () {
if (widget.uid == authController.user?.uid) {
authController.signOut();
} else {
controller.followUser();
}
},
child: Text(
widget.uid == authController.user?.uid
? 'Sign Out'
: controller.user['isFollowing']
? 'Unfollow'
: 'Follow',
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
),
),
),
),
),
const SizedBox(
height: 25,
),
// video list
GridView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: controller.user['thumbnails'].length,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1,
crossAxisSpacing: 5,
),
itemBuilder: (context, index) {
String thumbnail =
controller.user['thumbnails'][index];
return CachedNetworkImage(
imageUrl: thumbnail,
fit: BoxFit.cover,
);
},
)
],
),
),
],
),
),
),
);
});
}
}
[enter image description here][1] [1]: https://i.stack.imgur.com/IScNW.png
What I can do to fix this error
CodePudding user response:
You have to first check if controller.user['profilePhoto']
is null or not. To fix this issue, you can change this code:
CachedNetworkImage(
fit: BoxFit.cover,
imageUrl: controller.user['profilePhoto']??"",
height: 100,
width: 100,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(
Icons.error,
),
),
with this one:
controller.user['profilePhoto']==null?
const Icon(
Icons.error,
):
CachedNetworkImage(
fit: BoxFit.cover,
imageUrl: controller.user['profilePhoto'],
height: 100,
width: 100,
placeholder: (context, url) =>
const CircularProgressIndicator(),
errorWidget: (context, url, error) =>
const Icon(
Icons.error,
),
),
CodePudding user response:
The error resides here :
CachedNetworkImage(
...
imageUrl: controller.user['profilePhoto']??"",
...
),
If you set an empty string as "imageUrl", the error will show as the "CachedNetworkImage" will try to open an empty url. You need to prevent this to append, try to use a default image or show another widget when you don't have a correct url.