Skip to content

Instantly share code, notes, and snippets.

View truongsinh's full-sized avatar
:octocat:
Blessed

TruongSinh Tran-Nguyen truongsinh

:octocat:
Blessed
View GitHub Profile
@truongsinh
truongsinh / main.dart
Last active March 30, 2019 04:15
failing-as-cast
Object getInput() {
final dynamic input = [];
return input..add(2)..add(4)..add(55);
}
void main() async {
final List<int> result = getInput() as List<int>; // fail with `CastError: type 'List<dynamic>' is not a subtype of type 'List<int>'`
print(result);
}
@truongsinh
truongsinh / main.dart
Last active March 30, 2019 04:16
successful-cast
Object getInput() {
final dynamic input = [];
return input..add(2)..add(4)..add(55);
}
void main() async {
final List<int> result = (getInput() as List<dynamic>).cast<int>();
print(result);
}
@truongsinh
truongsinh / complicated-data-structure.dart
Created March 30, 2019 04:43
complicated-data-structure
class Person {
String name;
List<UsaAddress> addresses;
List<Person> supervisorOf;
}
class UsaAddress {
String streetNameAndNumber; // nevermind validation
String streetNameAndNumber2; // nevermind validation
String city; // nevermind validation
@truongsinh
truongsinh / person.proto
Last active March 30, 2019 10:21
person.proto
// protoc --dart_out=./lib/gen ./protos/person.proto
// protoc --swift_out=./ios/gen ./protos/person.proto
syntax = "proto3";
// `java_package` should match the package name you declare for `androidPackage` in your pubspec.yaml
option java_package = "pro.truongsinh.flutter.example.plugin_with_protobuf";;
message Person {
string name = 1;
@truongsinh
truongsinh / pubspec.yaml.diff
Last active March 31, 2019 09:19
pubspec.yaml.diff
diff --git a/pubspec.yaml b/pubspec.yaml
index 207089d..c069c82 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -8,6 +8,7 @@ environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
+ protobuf: any
flutter:
@truongsinh
truongsinh / plugin_with_protobuf.podspec.diff
Created March 31, 2019 09:20
plugin_with_protobuf.podspec.diff
diff --git a/ios/plugin_with_protobuf.podspec b/ios/plugin_with_protobuf.podspec
index 8b0c4dc..ba34356 100644
--- a/ios/plugin_with_protobuf.podspec
+++ b/ios/plugin_with_protobuf.podspec
@@ -15,7 +15,8 @@ A new flutter plugin project.
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
+ s.dependency 'SwiftProtobuf', '~> 1.4'
@truongsinh
truongsinh / SwiftPluginWithProtobufPlugin.swift.diff
Created March 31, 2019 09:28
SwiftPluginWithProtobufPlugin.swift.diff
diff --git a/ios/Classes/SwiftPluginWithProtobufPlugin.swift b/ios/Classes/SwiftPluginWithProtobufPlugin.swift
index e73d064..edbded6 100644
--- a/ios/Classes/SwiftPluginWithProtobufPlugin.swift
+++ b/ios/Classes/SwiftPluginWithProtobufPlugin.swift
@@ -1,6 +1,49 @@
import Flutter
import UIKit
+func getMyPerson() -> Person {
+ // abbr
@truongsinh
truongsinh / plugin_with_protobuf.dart.diff
Created March 31, 2019 09:31
plugin_with_protobuf.dart.diff
diff --git a/lib/plugin_with_protobuf.dart b/lib/plugin_with_protobuf.dart
index 87a1759..a59870f 100644
--- a/lib/plugin_with_protobuf.dart
+++ b/lib/plugin_with_protobuf.dart
@@ -1,13 +1,17 @@
-import 'dart:async';
+import 'dart:typed_data' show Uint8List;
-import 'package:flutter/services.dart';
+import 'package:flutter/services.dart' show MethodChannel;
@truongsinh
truongsinh / build.gradle.diff
Created March 31, 2019 09:39
build.gradle.diff
diff --git a/android/build.gradle b/android/build.gradle
index b0b0b97..d5c5c82 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -3,6 +3,7 @@ version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.71'
+ ext.protobufVersion = '0.8.8'
repositories {
@truongsinh
truongsinh / PluginWithProtobufPlugin.kt.diff
Created March 31, 2019 09:42
PluginWithProtobufPlugin.kt
diff --git a/android/src/main/kotlin/com/example/plugin_with_protobuf/PluginWithProtobufPlugin.kt b/android/src/main/kotlin/com/example/plugin_with_protobuf/PluginWithProtobufPlugin.kt
index 430bbe0..0969560 100644
--- a/android/src/main/kotlin/com/example/plugin_with_protobuf/PluginWithProtobufPlugin.kt
+++ b/android/src/main/kotlin/com/example/plugin_with_protobuf/PluginWithProtobufPlugin.kt
@@ -6,6 +6,48 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
+fun getMyPerson(): PersonOuterClass.Person {
+ return PersonOuterClass.Person.newBuilder()