如何在flutter中将缓存的图像添加到应用栏(使用cached_image_network)

时间:2019-11-02 10:57:11

标签: flutter dart

return Scaffold(
      appBar: AppBar(title: CachedNetworkImage(imageUrl: "https://cdn.pixabay.com/photo/2016/02/24/19/05/apples-1220574__340.png",
        width: 80.0,
        height: 80.0,


    )),

我使用了缓存的网络图像在Flutter应用栏中添加图像,但是它不起作用,请帮助

这是控制台错误

Compiler message:
../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/cached_network_image-1.1.2+1/lib/src/scaled_file_image.dart:45:24: Error: The method 'ScaledFileImage.load' has fewer positional arguments than those of overridden method 'ImageProvider.load'.
  ImageStreamCompleter load(ScaledFileImage key) {
                       ^
../../Flutter/packages/flutter/lib/src/painting/image_provider.dart:403:24: Context: This is the overridden method ('load').
  ImageStreamCompleter load(T key, DecoderCallback decode);
                       ^
../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/cached_network_image-1.1.2+1/lib/src/cached_network_image_provider.dart:53:24: Error: The method 'CachedNetworkImageProvider.load' has fewer positional arguments than those of overridden method 'ImageProvider.load'.
  ImageStreamCompleter load(CachedNetworkImageProvider key) {
                       ^
../../Flutter/packages/flutter/lib/src/painting/image_provider.dart:403:24: Context: This is the overridden method ('load').
  ImageStreamCompleter load(T key, DecoderCallback decode);
                       ^
Exception: Errors during snapshot creation: null
#0      KernelSnapshot.build (package:flutter_tools/src/build_system/targets/dart.dart:230:7)
<asynchronous suspension>
#1      _BuildInstance._invokeInternal (package:flutter_tools/src/build_system/build_system.dart:526:25)
<asynchronous suspension>
#2      _BuildInstance.invokeTarget.<anonymous closure> (package:flutter_tools/src/build_system/build_system.dart:481:35)
#3      new Future.sync (dart:async/future.dart:222:31)
#4      AsyncMemoizer.runOnce (package:async/src/async_memoizer.dart:43:45)
#5      _BuildInstance.invokeTarget (package:flutter_tools/src/build_system/build_system.dart:481:21)
<asynchronous suspension>
<asynchronous suspension>
#6      BuildSystem.build (package:flutter_tools/src/build_system/build_system.dart:419:36)
#7      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
#8      BuildSystem.build (package:flutter_tools/src/build_system/build_system.dart:400:28)
#9      buildWithAssemble (package:flutter_tools/src/bundle.dart:125:48)
#10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
#11     buildWithAssemble (package:flutter_tools/src/bundle.dart:99:31)
#12     BundleBuilder.build (package:flutter_tools/src/bundle.dart:75:11)
#13     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
#14     BundleBuilder.build (package:flutter_tools/src/bundle.dart:52:21)
#15     BuildBundleCommand.runCommand (package:flutter_tools/src/commands/build_bundle.dart:126:25)
#16     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
#17     BuildBundleCommand.runCommand (package:flutter_tools/src/commands/build_bundle.dart:97:42)
#18     FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:557:18)
#19     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#20     _rootRunUnary (dart:async/zone.dart:1132:38)
#21     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#22     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#23     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#24     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#25     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#26     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:552:7)
#27     _rootRun (dart:async/zone.dart:1124:13)
#28     _CustomZone.run (dart:async/zone.dart:1021:19)
#29     _CustomZone.runGuarded (dart:async/zone.dart:923:7)
#30     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
#31     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#32     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#33     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:116:13)
#34     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:173:5)

Failed to build bundle.

FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\user\Flutter\packages\flutter_tools\gradle\flutter.gradle' line: 794

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebugArm'.
> Process 'command 'C:\Users\user\Flutter\bin\flutter.bat'' finished with non-zero exit value 1



请帮助我找到解决方案。我想将缓存的网络映像添加到我的应用中。您有更好的解决方案吗?

请告诉我您是否想要完整的代码,我使用了缓存的网络图像在flutter应用程序栏中添加图像,但是它不起作用,请帮助

1 个答案:

答案 0 :(得分:3)

确保使用该库的最新版本。就我而言,我在cached_network_image: ^1.1.2中尝试了以下代码:pubspec.yaml,一切正常。

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'AppBar Demo',
      debugShowCheckedModeBanner: false,
      home: MyApp()
    ),
  );
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: CachedNetworkImage(
          imageUrl: "https://cdn.pixabay.com/photo/2016/02/24/19/05/apples-1220574__340.png",
          width: 80.0,
          height: 80.0,
        )
      ),
      body: Center(
        child: Container(
          child: Text("asd"),
        ),
      ),
    );
  }
}