簡體   English   中英

“緩存的網絡圖像 _ 沒有主機指定的 Url”錯誤

[英]"Cached Network Image _ No Host Specified Url" Error

我正在使用 cached.network 圖像加載來自 firebase 的圖像,如果圖像是 URL 是 null,它會加載一個帶有圖標的圓形頭像。

它在模擬器中不能正常工作

發生異常。 ArgumentError(無效參數:URI 中未指定主機)這是代碼

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

[在此處輸入圖片描述][1] [1]: https://i.stack.imgur.com/IScNW.png

我能做些什么來修復這個錯誤

您必須首先檢查controller.user['profilePhoto']是否為 null。 要解決此問題,您可以更改此代碼:

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,
        ),
    ),

有了這個:

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,
    ),
  ),

錯誤駐留在此處:

 CachedNetworkImage(
      ...
      imageUrl: controller.user['profilePhoto']??"",
      ...
  ),

如果您將空字符串設置為“imageUrl”,則錯誤將顯示為“CachedNetworkImage”將嘗試打開一個空的 url。您需要防止此為 append,嘗試使用默認圖像或在您不這樣做時顯示另一個小部件沒有正確的 url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM