GitHub Copilot suggested code for List.of type cast error.

GitHub Copilot is pretty AWESOME!!

Just taking a few minutes to express a little nuanced gratitude for the GitHub Copilot AI tool.

Practically speaking, in working with Dart, when creating shallow copies of Lists and Maps, I recently learned a primary difference between .from() and .of():

Using .from() will yield runtime exceptions while using .of() will reveal any potential type casting errors at compile time (i.e. while you’re working on the code — where you would most likely prefer to see it).

In switching .from( to .of(, there can be errors shown that otherwise would have not revealed themselves until they were touched during runtime. In one such case, I added the lint error as a comment, commented the offending code, turned on GitHub Copilot, and hit enter at the end of the commented line.

Copilot did the rest.

Although this could prove to make me syntax-challenged at some point by not walking through creating the List or Map myself, sometimes the simplicity of a useful tool allowing you to get you on your way in just a couple seconds, IMO, is worth the trade off in short-term productivity.

final alertMsgStr = List<String>.of(
  // statusObj['message']?.toList() ?? <String>['Missing message.'],
  // Error: The argument type 'List<Object?>' can't be assigned to 
  //        the parameter type 'Iterable<String>'. argument_type_not_assignable
  statusObj['message']?.map((e) => e.toString()) ?? <String>['Missing message.'],
);

This is a tiny representation of what GitHub Copilot has to offer, for sure, but sometimes you gotta stop and 👃 the 🌹.

Here are a few additional insights into .from() and .of() which explain it much better than this post.

Cheers!
– Keith | https://keithdc.com

Leave a Reply