Valid Data Provider for spark List component

Posted: June 8, 2011 in Adobe, Flex, Technology
Tags: , , , , ,

In the course of migration from Flex3 project to Flex4 usually we faced common TypeError at runtime. These error are generally a type coercion failed error. The simple usecase of this is we are converting <mx:List> component to <s:List> component. Here we have an inline xml object for “imgList” and binding it to a <mx:List> component.

It was working fine but after changing the <mx:List> component into <s:List> we are faced runtime TypeError like this:

|TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@284a62a9 to mx.collections.IList.

Runtime error
<s:List labelField=”@name” dataProvider=”{ imgList.image }”/>

Works fine
<mx:List labelField=”@name” dataProvider=”{ imgList.image}”/>

The xml object of imagelist is as such:
var imgList:XML = ‘<imagelist>
<image name=”Image 1” source=”assets/image_01.jpg”/>

The <mx:List> component could take a few different objects as dataproviders, but the <s:List> spark List is typed only for taking IList type of object. The fact that it’s named “List”, XMLList turned out to be a top level Flash object that doesn’t implement the mx.collection.IList interface, which is required for a data source to be qualified as a DataProvider. The commonly used classes that implement IList are ArrayCollection, ArrayList (curiously) and XMLListCollection.

So the solution of this is to we need to typecast dataprovider data object into IList type.
dataProvider=”{new XMLListCollection(imageList.image)}”

<s:List labelField=”@name” dataProvider=”{new XMLListCollection(XMLList(imageList.image))}”/>

For more information on collection and data provider check this page :

P.S. – The code written in post are only for reference purpose.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s