Flutter之pigeon

资源

pigeon: ^1.0.17
packages/packages/pigeon
Flutter官方推荐插件开发辅助工具-Pigeon

安装

dart pub add pigeon

pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  pigeon: ^1.0.7
  ...

步骤

1. 创建一个messages.dart模版文件

messages.dart

import 'package:pigeon/pigeon.dart';

class Book {
  String? title;
  String? author;
}

@HostApi()
abstract class BookApi {
  List<Book?> search(String keyword);
}

使用命令创建文件

./flutter pub run pigeon --input lib/messages.dart --dart_out lib/pigeon.dart --objc_header_out ios/Runner/pigeon.h --objc_source_out ios/Runner/pigeon.m --java_out android/app/src/main/java/top/ovo/flutterame/Test.java --java_package "top.ovo.flutterame"

自动生成的文件

IOS生成的文件

pigeon.h

// Autogenerated from Pigeon (v1.0.17), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#import <Foundation/Foundation.h>
@protocol FlutterBinaryMessenger;
@protocol FlutterMessageCodec;
@class FlutterError;
@class FlutterStandardTypedData;

NS_ASSUME_NONNULL_BEGIN

@class Book;

@interface Book : NSObject
+ (instancetype)makeWithTitle:(nullable NSString *)title
    author:(nullable NSString *)author;
@property(nonatomic, copy, nullable) NSString * title;
@property(nonatomic, copy, nullable) NSString * author;
@end

/// The codec used by BookApi.
NSObject<FlutterMessageCodec> *BookApiGetCodec(void);

@protocol BookApi
- (nullable NSArray<Book *> *)searchKeyword:(NSString *)keyword error:(FlutterError *_Nullable *_Nonnull)error;
@end

extern void BookApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<BookApi> *_Nullable api);

NS_ASSUME_NONNULL_END

pigeon.m

// Autogenerated from Pigeon (v1.0.17), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#import "pigeon.h"
#import <Flutter/Flutter.h>

#if !__has_feature(objc_arc)
#error File requires ARC to be enabled.
#endif

static NSDictionary<NSString *, id> *wrapResult(id result, FlutterError *error) {
  NSDictionary *errorDict = (NSDictionary *)[NSNull null];
  if (error) {
    errorDict = @{
        @"code": (error.code ? error.code : [NSNull null]),
        @"message": (error.message ? error.message : [NSNull null]),
        @"details": (error.details ? error.details : [NSNull null]),
        };
  }
  return @{
      @"result": (result ? result : [NSNull null]),
      @"error": errorDict,
      };
}
static id GetNullableObject(NSDictionary* dict, id key) {
  id result = dict[key];
  return (result == [NSNull null]) ? nil : result;
}


@interface Book ()
+ (Book *)fromMap:(NSDictionary *)dict;
- (NSDictionary *)toMap;
@end

@implementation Book
+ (instancetype)makeWithTitle:(nullable NSString *)title
    author:(nullable NSString *)author {
  Book* pigeonResult = [[Book alloc] init];
  pigeonResult.title = title;
  pigeonResult.author = author;
  return pigeonResult;
}
+ (Book *)fromMap:(NSDictionary *)dict {
  Book *pigeonResult = [[Book alloc] init];
  pigeonResult.title = GetNullableObject(dict, @"title");
  pigeonResult.author = GetNullableObject(dict, @"author");
  return pigeonResult;
}
- (NSDictionary *)toMap {
  return [NSDictionary dictionaryWithObjectsAndKeys:(self.title ? self.title : [NSNull null]), @"title", (self.author ? self.author : [NSNull null]), @"author", nil];
}
@end

@interface BookApiCodecReader : FlutterStandardReader
@end
@implementation BookApiCodecReader
- (nullable id)readValueOfType:(UInt8)type 
{
  switch (type) {
    case 128:     
      return [Book fromMap:[self readValue]];
    
    default:    
      return [super readValueOfType:type];
    
  }
}
@end

@interface BookApiCodecWriter : FlutterStandardWriter
@end
@implementation BookApiCodecWriter
- (void)writeValue:(id)value 
{
  if ([value isKindOfClass:[Book class]]) {
    [self writeByte:128];
    [self writeValue:[value toMap]];
  } else 
{
    [super writeValue:value];
  }
}
@end

@interface BookApiCodecReaderWriter : FlutterStandardReaderWriter
@end
@implementation BookApiCodecReaderWriter
- (FlutterStandardWriter *)writerWithData:(NSMutableData *)data {
  return [[BookApiCodecWriter alloc] initWithData:data];
}
- (FlutterStandardReader *)readerWithData:(NSData *)data {
  return [[BookApiCodecReader alloc] initWithData:data];
}
@end

NSObject<FlutterMessageCodec> *BookApiGetCodec() {
  static dispatch_once_t sPred = 0;
  static FlutterStandardMessageCodec *sSharedObject = nil;
  dispatch_once(&sPred, ^{
    BookApiCodecReaderWriter *readerWriter = [[BookApiCodecReaderWriter alloc] init];
    sSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter];
  });
  return sSharedObject;
}


void BookApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<BookApi> *api) {
  {
    FlutterBasicMessageChannel *channel =
      [FlutterBasicMessageChannel
        messageChannelWithName:@"dev.flutter.pigeon.BookApi.search"
        binaryMessenger:binaryMessenger
        codec:BookApiGetCodec()];
    if (api) {
      NSCAssert([api respondsToSelector:@selector(searchKeyword:error:)], @"BookApi api (%@) doesn't respond to @selector(searchKeyword:error:)", api);
      [channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
        NSArray *args = message;
        NSString *arg_keyword = args[0];
        FlutterError *error;
        NSArray<Book *> *output = [api searchKeyword:arg_keyword error:&error];
        callback(wrapResult(output, error));
      }];
    }
    else {
      [channel setMessageHandler:nil];
    }
  }
}

Android生成的文件

Pigeon.java

// Autogenerated from Pigeon (v1.0.17), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package top.ovo.flutterame;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StandardMessageCodec;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/** Generated class from Pigeon. */
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression"})
public class Pigeon {

  /** Generated class from Pigeon that represents data sent in messages. */
  public static class Book {
    private @Nullable String title;
    public @Nullable String getTitle() { return title; }
    public void setTitle(@Nullable String setterArg) {
      this.title = setterArg;
    }

    private @Nullable String author;
    public @Nullable String getAuthor() { return author; }
    public void setAuthor(@Nullable String setterArg) {
      this.author = setterArg;
    }

    public static class Builder {
      private @Nullable String title;
      public @NonNull Builder setTitle(@Nullable String setterArg) {
        this.title = setterArg;
        return this;
      }
      private @Nullable String author;
      public @NonNull Builder setAuthor(@Nullable String setterArg) {
        this.author = setterArg;
        return this;
      }
      public @NonNull Book build() {
        Book pigeonReturn = new Book();
        pigeonReturn.setTitle(title);
        pigeonReturn.setAuthor(author);
        return pigeonReturn;
      }
    }
    @NonNull Map<String, Object> toMap() {
      Map<String, Object> toMapResult = new HashMap<>();
      toMapResult.put("title", title);
      toMapResult.put("author", author);
      return toMapResult;
    }
    static @NonNull Book fromMap(@NonNull Map<String, Object> map) {
      Book pigeonResult = new Book();
      Object title = map.get("title");
      pigeonResult.setTitle((String)title);
      Object author = map.get("author");
      pigeonResult.setAuthor((String)author);
      return pigeonResult;
    }
  }
  private static class BookApiCodec extends StandardMessageCodec {
    public static final BookApiCodec INSTANCE = new BookApiCodec();
    private BookApiCodec() {}
    @Override
    protected Object readValueOfType(byte type, ByteBuffer buffer) {
      switch (type) {
        case (byte)128:         
          return Book.fromMap((Map<String, Object>) readValue(buffer));
        
        default:        
          return super.readValueOfType(type, buffer);
        
      }
    }
    @Override
    protected void writeValue(ByteArrayOutputStream stream, Object value)     {
      if (value instanceof Book) {
        stream.write(128);
        writeValue(stream, ((Book) value).toMap());
      } else 
{
        super.writeValue(stream, value);
      }
    }
  }

  /** Generated interface from Pigeon that represents a handler of messages from Flutter.*/
  public interface BookApi {
    List<Book> search(String keyword);

    /** The codec used by BookApi. */
    static MessageCodec<Object> getCodec() {
      return BookApiCodec.INSTANCE;
    }

    /** Sets up an instance of `BookApi` to handle messages through the `binaryMessenger`. */
    static void setup(BinaryMessenger binaryMessenger, BookApi api) {
      {
        BasicMessageChannel<Object> channel =
            new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.BookApi.search", getCodec());
        if (api != null) {
          channel.setMessageHandler((message, reply) -> {
            Map<String, Object> wrapped = new HashMap<>();
            try {
              ArrayList<Object> args = (ArrayList<Object>)message;
              String keywordArg = (String)args.get(0);
              if (keywordArg == null) {
                throw new NullPointerException("keywordArg unexpectedly null.");
              }
              List<Book> output = api.search(keywordArg);
              wrapped.put("result", output);
            }
            catch (Error | RuntimeException exception) {
              wrapped.put("error", wrapError(exception));
            }
            reply.reply(wrapped);
          });
        } else {
          channel.setMessageHandler(null);
        }
      }
    }
  }
  private static Map<String, Object> wrapError(Throwable exception) {
    Map<String, Object> errorMap = new HashMap<>();
    errorMap.put("message", exception.toString());
    errorMap.put("code", exception.getClass().getSimpleName());
    errorMap.put("details", "Cause: " + exception.getCause() + ", Stacktrace: " + Log.getStackTraceString(exception));
    return errorMap;
  }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,关于Flutter中的DataTable使用详解,我可以为您提供以下内容: 1. DataTable简介 DataTable是Flutter中的一个控件,它可以用于显示表格数据。DataTable是由多个TableRow组成的,每个TableRow代表一行数据,每个TableCell代表一个单元格。 2. DataTable使用方法 a. 创建一个DataTable 创建一个DataTable需要使用DataTable构造函数,并传入两个必要参数:columns和rows。 columns代表表格的列,它是一个列表,每个元素都是一个DataColumn对象。DataColumn有两个必要参数:label和tooltip,分别代表列标题和列提示。 rows代表表格的数据,它是一个列表,每个元素都是一个DataRow对象。DataRow有两个必要参数:cells和selected,分别代表行数据和是否选中。 b. 设置DataTable属性 DataTable还有一些可选属性,可以用于自定义表格的样式和行为。例如: sortColumn:可以设置表格默认排序的列。 sortAscending:可以设置表格默认排序的顺序。 onSelectAll:可以设置全选和取消全选的回调函数。 3. DataTable示例 下面是一个简单的DataTable示例: ``` DataTable( columns: [ DataColumn(label: Text('Name')), DataColumn(label: Text('Age')), DataColumn(label: Text('Gender')), ], rows: [ DataRow(cells: [ DataCell(Text('Alice')), DataCell(Text('18')), DataCell(Text('Female')), ]), DataRow(cells: [ DataCell(Text('Bob')), DataCell(Text('20')), DataCell(Text('Male')), ]), DataRow(cells: [ DataCell(Text('Charlie')), DataCell(Text('22')), DataCell(Text('Male')), ]), ], ) ``` 以上就是Flutter中DataTable的使用方法和示例。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵健zj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值