neděle 18. prosince 2016

Dart Firebase3 lib is now Firebase

Just a quick update on Firebase Dart library.



My library Firebase3 has been deprecated and moved to the official Firebase repo (yay!) and is going to be developed there.

The update in your project is really easy, so please do it:

pubspec.yaml

Update your project's pubspec.yaml file. Change the firebase3 dependency to firebase:

dependencies:
  firebase: '^3.0.0'

Pub get

Do the pub get to get the latest dependencies.


Imports

Update imports in your project to use the firebase library:


import 'package:firebase/firebase.dart';


And you are done!

The Dart summit Firebase project has been already updated for the latest firebase lib.

Happy coding!  :-)

čtvrtek 3. listopadu 2016

Dart Developer Summit 2016

I was happy that I could attend this year's Dart Dev Summit because the last Dart Dev Summit was awesome.

The Dart Developer Summit 2016 took place in Munich's Google office this year, for two days at the end of October. And it was really worth to visit.



New Dart earrings

Couple of days before a summit, I realized that my current Dart earrings are a bit of outdated. We had so much fun together but the update was inevitable. And I knew I wanted to have them 3D printed.

In Saturday afternoon, I decided "now or never" and opened the 3D modeling software after, ehm, 5 years and started to remember how it worked. I forgot almost everything. Fortunately I somehow managed to have the model finished (after a lots of complaints :-D) and we could go to print!

We had to do a couple of iterations but at the end I had the earrings printed and put together. Let's call them Dart earrings 2 RC1 for now.

Dart earrings: the old vs. the new version

The earrings need some iterations still. And I am planning to release the 3D model when they will be perfect ;-)

Dart Code Lab

The evening before the summit, I helped with the organization of the Dart Code Lab again. Attendees could try two new Dart code labs - the first one on AngularDart components and the second one on the developer workflow.



We had also a lots of awesome demos from attendees showing us the projects they built with Dart. I enjoyed the evening with people from the community very much.

Dart Developer Summit

The Dart summit was two days event. The first day started with the keynote - Dart in 2017 and beyond where we could hear a lots of interesting numbers (did you know that there is more than $70B per year in Google applications written in Dart?) and about the actual Dart's state and focus. You can read more about this in the Dart news blog post.

Dart Dev Summit 2017 - on a boat? :-D

The summit then continued with a lots of interesting talks on various topics like Angular 2 Dart and it's components, the set of material design widgets written in AngularDart for developers.

We also heard about Dart's type system and a strong mode, the Dart Dev Compiler (yay!), and about the Dart to JavaScript interoperability and more. The JS interop talk is a definitely "must see" if you consider writing a wrapper around some existing JS library.

The conferences are not only about talks. We had a lot of space to meet and talk with Dart engineers and developers from the whole world.

I must also say that I loved the setup and decorations in the room.

Darts everywhere...

The second day started with the Flutter keynote. Flutter is a project which helps you to build both iOS and Android applications with Dart.

Flutter Flutter Flutter

Over the year, they moved the project (it was named Sky before) so much forward and with the hot reload feature, the development is even faster and more delightful to developer.

Flutter's hot reload: must see

I was very impressed and I am moving my personal todo "try Flutter" more to the top of my todo list. :-D

During the day we could see more talks on for example Google's build tool Bazel, server-side framework Aqueduct or Dart package for React.

The day ended with a panel discussion where we could ask questions and the Dart team answered.

All the videos from talks are online in the Dart Dev Summit 2016 playlist.

My Firebase talk

I was excited to have a talk on How to build a Dart and Firebase app in 30 mins. During the talk I showed what is a Firebase and how to use this service with Dart using the wrapper library Firebase3, I implemented. The library is available on pub and Github.

I am planning to write some article on how to use Dart + Firebase and how to write a JS interop wrapper soon.

For now you can take a look at the talk on Youtube, explore the slides, or the source code for the demo app I showed.

The demo is available online, the source code is on Github.

The demo is publicly available and is written in Dart using the Firebase library, then compiled and deployed to Firebase hosting.

The end

It's always so sad when such great event ends. The Dart Developer Summit was a really great event, I met a lots of Dart developers and saw really nice talks on topics which interest me. I filled my personal todo list with a lots of "must try" or "must explore" things.

I am really looking forward to the next year's summit and see ya on a boat! :-D

čtvrtek 7. ledna 2016

Dart RPC: Create RESTful API in a couple of minutes

I am on the front-end mostly because I like to work with UI. But let's move on and try something new, a little bit of server-side...


In this blog post I show you how to create your RESTful server-side API in Dart really fast! :-)

How to start

There is a really nice library on pub called RPC. It is a light-weight package for creating RESTful server-side Dart APIs. And this library is everything we need.

Let's start with adding dependency into pubspec.yaml in our project:

dependencies:
  rpc: "^0.5.5"

And then call pub get to get the package into our project.
Now you can import it.

import 'package:rpc/rpc.dart';

Create API class

We need to create API class which wraps our API. It is a Dart class with special @ApiClass annotation from the RPC library. The annotation has one mandatory parameter - version where we provide a version for our API. For example v1. You can also optionally provide other parameters like name to be able to rename the API.

@ApiClass(version: 'v1')
class MyApi {
  // here will be contents of the class.
}

API methods

We want to be able to provide API methods like GET, POST, DELETE, etc. We put them in our already prepared API class with special annotation.

Methods can be put directly into our API class or API resources can be used.

Methods in API class

Here we create the GET all animals and POST animal method. Every API method has to have the @ApiMethod annotation with mandatory path parameter where we specify the path which is used for calling of our method. We can also specify the method parameter where we provide type of the method which will be used. If the method parameter is omitted, the GET is used.

@ApiClass(version: 'v1')
class MyApi {
  List _animals = [];

  @ApiMethod(path: 'animals')
  List<Animal> getAnimals() => _animals;

  @ApiMethod(path: 'animals', method: 'POST')
  Animal postAnimal(Animal animal) {
    _animals.add(animal);
    return animal;
  }
}

We are using List for saving of our animals in this example. And we provide two methods: getAnimals() which returns a list of all animals and postAnimal() which saves a new animal into our list. Easy. Of course we could extend our example API with other methods.

To have it working we also need to have our Animal class:

class Animal {
  int id;
  String name;
  int numberOfLegs;
}

API resources

Usually we want to divide our API into resources which wrap the particular API methods. We want to for example have one API resource for an Animal and one API resource for a Person. We can do this with the RPC library easily.

Create the resource class and move all the resource methods in it:

class AnimalResource {
  List _animals = [];

  @ApiMethod(path: 'animals')
  List<Animal> getAnimals() => _animals;

  @ApiMethod(path: 'animals', method: 'POST')
  Animal postAnimal(Animal animal) {
    _animals.add(animal);
    return animal;
  }
}

And then create the AnimalResource instance into our API class with @ApiResource annotation:

@ApiClass(version: 'v1')
class MyApi {
  @ApiResource()
  AnimalResource resource = new AnimalResource();
}

Test the API

To be able to use and test our API, we need to create the server script in bin/ folder. We need to create the ApiServer there, add it our API and create a new HttpServer which starts listening on localhost:8080.

library my_server;

import 'dart:io';
import 'package:logging/logging.dart';
import 'package:rpc/rpc.dart';
import '../lib/server/api.dart';

final ApiServer apiServer = new ApiServer(prettyPrint: true);

main() async {
  Logger.root..level = Level.INFO..onRecord.listen(print);

  apiServer.addApi(new MyApi());
  HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080);
  server.listen(apiServer.httpRequestHandler);
  print('Server listening on http://${server.address.host}: ${server.port}');
}

Then run the Dart script in bin/ which starts the server and explore the API methods. Our API is available on localhost:8080/myApi/v1. If we want to get a list of all animals, we call GET on localhost:8080/myApi/v1/animals. Or we can try the POST for the new animal on the same address localhost:8080/myApi/v1/animals.

Trying a POST method is more difficult because we need to either install a tool like curl or we can use some extension to Chrome (like Advanced REST client or Postman).

And that's all! Now you have a really simple Dart API created in a couple of minutes.

Where can I find more?

This library can do even more. You can find all information about this library on the official RPC's site on pub or on Github.

My server Code Lab

I was happy to be and talk at couple of DevFests last year, where I had also a Dart code lab. My code lab was about server-side and as you could guess, I used the RPC library :-) You can take a look at and try the code lab on my Github.

Happy coding!