Flutter Share软件包导致无用的错误

时间:2018-11-22 00:27:24

标签: dart flutter

我正在尝试使用Flutter团队提供的Share软件包。我尝试实现该程序包,当该程序不起作用时,我尝试直接复制并粘贴其代码,但同样出现错误。不幸的是,该错误不是很有帮助,错误的描述为“我们需要更好地解释这一点”。有什么想法吗?

Here's the package I'm using

我的代码:

FlatButton(
    child: Text(
        'Share',
        style: TextStyle(
            color: Color.fromRGBO(245, 93, 62, 1.0)),
    ),
    color: Colors.grey[100],
    onPressed: () {
        final RenderBox box = context.findRenderObject();
        Share.share('Hello this is a test',
            sharePositionOrigin:
                box.localToGlobal(Offset.zero) &
                box.size);
    },
),

错误:

flutter: The following assertion was thrown while handling a gesture:
flutter: type 'RenderSliverList' is not a subtype of type 'RenderBox'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      _EverythingState._buildEventCards.<anonymous closure> (package:loopt_in/widgets/everything.dart:175:43)
flutter: #1      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
flutter: #2      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
flutter: #3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
flutter: #4      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
flutter: #5      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
flutter: #6      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
flutter: #7      _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:184:20)
flutter: #8      _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
flutter: #9      _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
flutter: #10     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
flutter: #12     _invoke1 (dart:ui/hooks.dart:165:13)
flutter: #13     _dispatchPointerDataPacket (dart:ui/hooks.dart:119:5)
flutter: Handler: onTap
flutter: Recognizer:
flutter:   TapGestureRecognizer#ecc56(debugOwner: GestureDetector, state: ready, won arena, finalPosition:
flutter:   Offset(196.0, 747.5), sent tap down)

更新

import 'package:flutter/material.dart';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:share/share.dart';

class Everything extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _EverythingState();
  }
}

class _EverythingState extends State<Everything> {
  Widget _buildEventCards(BuildContext context, DocumentSnapshot document) {
    var width = MediaQuery.of(context).size.width;

    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 14.0),
      child: Card(
        elevation: 0.0,
        child: Column(
          children: <Widget>[
            Image.asset(
              document['image'],
              fit: BoxFit.cover,
            ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0),
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 15.0,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          // Icon(document['icon']),
                        ],
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10.0,
                  ),
                  Wrap(
                    direction: Axis.horizontal,
                    alignment: WrapAlignment.start,
                    runAlignment: WrapAlignment.start,
                    children: <Widget>[
                      Chip(
                        backgroundColor: Colors.grey[100],
                        label: Text(
                          document['date'],
                        ),
                      ),
                      SizedBox(
                        width: 10.0,
                      ),
                      Chip(
                        backgroundColor: Colors.grey[100],
                        label: Text(document['source']),
                      ),
                      SizedBox(
                        width: 10.0,
                      ),
                      Chip(
                        backgroundColor: Colors.grey[100],
                        label: Text(
                          document['location'],
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 15.0,
                  ),
                  Container(
                    width: width,
                    child: Text(
                      document['title'],
                      style: TextStyle(fontSize: 24.0),
                    ),
                  ),
                  SizedBox(
                    height: 20.0,
                  ),
                  Text(document['desc']),
                  SizedBox(
                    height: 20.0,
                  ),
                  Row(
                    children: <Widget>[
                      FlatButton(
                        child: Text(
                          'More',
                          style: TextStyle(
                              color: Color.fromRGBO(118, 190, 208, 1.0)),
                        ),
                        color: Colors.grey[100],
                        onPressed: () {
                          print('webview clicked');
                          Navigator.of(context).push(
                            MaterialPageRoute(
                              builder: (context) => WebviewScaffold(
                                    url: document['url'],
                                    appBar: AppBar(
                                      title: Text(document['source']),
                                      backgroundColor: Color.fromRGBO(
                                        135,
                                        142,
                                        136,
                                        1.0,
                                      ),
                                    ),
                                  ),
                            ),
                          );
                        },
                      ),
                      SizedBox(
                        width: 15.0,
                      ),
                      FlatButton(
                        child: Text(
                          'Share',
                          style: TextStyle(
                              color: Color.fromRGBO(245, 93, 62, 1.0)),
                        ),
                        color: Colors.grey[100],
                        onPressed: () {
                          final RenderBox box = context.findRenderObject();
                          Share.share('Hello this is a test',
                                  sharePositionOrigin:
                                      box.localToGlobal(Offset.zero) &
                                          box.size);
                        },
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: Firestore.instance.collection('stories').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return const Text('Loading...');
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
                _buildEventCards(context, snapshot.data.documents[index]),
          );
        });
  }

  // @override
  // void initState() {
  //   // TODO: implement initState
  //   super.initState();

  //   FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
  //   var bannerAd = _buildBanner()..load();
  // }
}

新错误回溯

[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
MissingPluginException(No implementation found for method share on channel plugins.flutter.io/share)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
<asynchronous suspension>
#1      Share.share (package:share/share.dart:44:20)
#2      ShareButton.build.<anonymous closure> (package:loopt_in/widgets/everything.dart:220:17)
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
#4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)

2 个答案:

答案 0 :(得分:1)

此行中的问题

namespace Cardinity.Infrastructure
{
    using System.Security.Cryptography;
    using System;
    enum EncryptionMethods
    {
        None=0,
        HMACSHA1,
        HMACSHA256,
        HMACSHA384,
        HMACSHA512,
        HMACMD5
    }


internal class Protected
{
    private  Byte[] salt = Guid.NewGuid().ToByteArray();

    protected byte[] Protect(byte[] data)
    {
        try
        {
            return ProtectedData.Protect(data, salt, DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException)//no reason for hackers to know it failed
        {
#if DEBUG
            throw;
#else
            return null;
#endif
        }
    }

    protected byte[] Unprotect(byte[] data)
    {
        try
        {
            return ProtectedData.Unprotect(data, salt, DataProtectionScope.CurrentUser);
        }
        catch (CryptographicException)//no reason for hackers to know it failed
        {
#if DEBUG
            throw;
#else
            return null;
#endif
        }
    }
}


    internal class SecretKeySpec:Protected,IDisposable
    {
        readonly EncryptionMethods _method;

        private byte[] _secretKey;
        public SecretKeySpec(byte[] secretKey, EncryptionMethods encryptionMethod)
        {
            _secretKey = Protect(secretKey);
            _method = encryptionMethod;
        }

        public EncryptionMethods Method => _method;
        public byte[] SecretKey => Unprotect( _secretKey);

        public void Dispose()
        {
            if (_secretKey == null)
                return;
            //overwrite array memory
            for (int i = 0; i < _secretKey.Length; i++)
            {
                _secretKey[i] = 0;
            }

            //set-null
            _secretKey = null;
        }
        ~SecretKeySpec()
        {
            Dispose();
        }
    }

    internal class Mac : Protected,IDisposable
    {
        byte[] rawHmac;
        HMAC mac;
        public Mac(SecretKeySpec key, string data)
        {

            switch (key.Method)
            {
                case EncryptionMethods.HMACMD5:
                    mac = new HMACMD5(key.SecretKey);
                    break;
                case EncryptionMethods.HMACSHA512:
                    mac = new HMACSHA512(key.SecretKey);
                    break;
                case EncryptionMethods.HMACSHA384:
                    mac = new HMACSHA384(key.SecretKey);
                    break;
                case EncryptionMethods.HMACSHA256:
                    mac = new HMACSHA256(key.SecretKey);

                break;
                case EncryptionMethods.HMACSHA1:
                    mac = new HMACSHA1(key.SecretKey);
                    break;

                default:                    
                    throw new NotSupportedException("not supported HMAC");
            }
            rawHmac = Protect( mac.ComputeHash(Cardinity.ENCODING.GetBytes(data)));            

        }

        public string AsBase64()
        {
            return System.Convert.ToBase64String(Unprotect(rawHmac));
        }

        public void Dispose()
        {
            if (rawHmac != null)
            {
                //overwrite memory address
                for (int i = 0; i < rawHmac.Length; i++)
                {
                    rawHmac[i] = 0;
                }

                //release memory now
                rawHmac = null;

            }
            mac?.Dispose();
            mac = null;

        }
        ~Mac()
        {
            Dispose();
        }
    }
}

右侧返回RenderSliv​​erList,然后将其分配给RenderBox,以说明为什么存在不匹配类型。

您到底想分享什么?所以我可以帮忙

编辑:

您需要将共享按钮提取到其自己的小部件中。尝试下面的完整代码。可以。

RenderBox box = context.findRenderObject();

答案 1 :(得分:0)

万一仍然有人对此有疑问。这是对我有用的代码。

shareData(BuildContext context){
  Share.share('Some text here', 
    subject: 'Update the coordinate!',
    sharePositionOrigin: Rect.fromLTWH(left, top, width, height) 
  );
}