How to use dart-protobuf

I am considering using dart-protobuf instead of JSON in one of my projects. The problem is that the library does not provide any example of how to use it, and the tests also do not help.

I'm also a bit confused about how parsing .proto files will work.

So, I'm looking for a simple example of how to use this library in a dart.

+3
dart protocol-buffers
Feb 14 '14 at 3:44
source share
2 answers

I'm not too familiar with dart-protobuf, but it looks like you need to use the protobuf compiler and the dart-protoc-plugin project to create your prototype Dart library from the proto-definition.

Here are some instructions: https://github.com/dart-lang/dart-protoc-plugin

+3
Feb 14 '14 at 4:03
source share

I use it and it is awesome. Below was the hardest part for me (de / serialization). Perhaps the docs are better now.

send a request ( query is a protocol buffer object to send)

 request.send(query.writeToBuffer()); 

get response ( pb.MovieMessage is a protocol buffer object for deserializing the response)

 request.onLoad.listen((ProgressEvent e) { if ((request.status >= 200 && request.status < 300) || request.status == 0 || request.status == 304) { List<int> buffer = new Uint8List.view(request.response); var response = new pb.MovieMessage.fromBuffer(buffer); 

EDIT

My method of sending a PB request to the server

 Future<pb.MovieMessage> send(pb.MovieMessage query) { var completer = new Completer<pb.MovieMessage>(); var uri = Uri.parse("http://localhost:8080/public/data/"); var request = new HttpRequest() ..open("POST", uri.toString(), async: true) ..overrideMimeType("application/x-google-protobuf") ..setRequestHeader("Accept", "application/x-google-protobuf") ..setRequestHeader("Content-Type", "application/x-google-protobuf") ..responseType = "arraybuffer" ..withCredentials = true // seems to be necessary so that cookies are sent ..onError.listen((e) { completer.completeError(e); }) ..onProgress.listen((e){}, onError:(e) => _logger.severe("Error: " + e.errorMessage)); request.onReadyStateChange.listen((e){}, onError: (e) => _logger.severe("OnReadyStateChange.OnError: " + e.toString()) ); request.onLoad.listen((ProgressEvent e) { if ((request.status >= 200 && request.status < 300) || request.status == 0 || request.status == 304) { List<int> buffer = new Uint8List.view(request.response); var response = new pb.MovieMessage.fromBuffer(buffer); response.errors.forEach((pb.Error e) => _logger.severe("Error: " + e.errorMessage)); completer.complete(response); } else { completer.completeError(e); } }); request.send(query.writeToBuffer()); return completer.future; } 
+5
Feb 14 '14 at 5:24
source share



All Articles